tags:

views:

168

answers:

4

This is one of those 'oh my god' moments.

The previous programmer in our company has left behind a badly-coded application using PHP and MySQL.

One example is that he has stored options for each customer as a comma separated value in MySQL. The entire application is NOT OOP based and so there are repeated references and queries in almost every page of the 500+ pages of PHP. So it's not an easy job now to change the schema and data storage logics.

To adjust the system for a matter of six months, I am looking for a way to perform a search on those comma-separates values. Has someone got any idea about searching such CSV without much performance impact? I know it's not going to be the best, but at least I could push the application to go for another six months before the new application is ready.

Thank you for any help

+2  A: 

What about creating a table that correctly breaks the CSV column into multiple columns (or multiple records) once as a batch process, and then cobbling together an update trigger on the old table to refresh the entry (or entries) for that user? Then you could write decent queries across it without having to re-code the setter logic.

Ry4an
That's an excellent idea! That even makes life easier to write _temporary_ codes and still maintain atomicity in the new table.
Nirmal
A: 

Here's one idea for handling it without changing the schema or representation:

<?php
function checkoption ($user, $option)
{
    global $db;
    $rs = mysql_query ("select option_field_name from customer where user = '$user'", $db);
    if ($rs)
    {
         // enclose result in commas to remove edge cases for next test
         $opts = ",," . $rs ['option_field_name'] . ",";
         return strpos ($opts, ",$option,") != false;
    }
    return false;
}
?>

It looks for ,option, in the field for the user, and guards against edge cases by handling all references with an extra pair of surrounding commas. To avoid the zero versus Null problem, an extra comma is added to the front of the db string.

wallyk
A: 

So you're saying you want a query that returns all rows where a user has a specific option?

Well, ugh...okay, this isn't pretty, but you already know that, so:

select * from user_table
   where concat(',',option_field,',') like "%,option_you_want,%"

I can't speak to the performance impact--it's likely not pleasant, but I think your other option is to break out the row into a separate table, but you're saying that's not realistically available to you.

spinn
+3  A: 

You could use FIND_IN_SET to retrieve rows that match your criteria:

SELECT * FROM your_table WHERE FIND_IN_SET('value', field_with_comma_sep_values) > 0;

Basically, FIND_IN_SET returns the index of the found item. So this query finds all rows where it finds the matching word in the "set" of comma separated values.

Credit: I knew there was something like this, but this post is where I found the answer and the SELECT statement.

Doug Neiner