如何在SQL字段中处理数组?

I have a feed that comes from the State of Florida in a CSV that I need to load daily into MySQL. It is a listing of all homes for sale in my area. One field has a list of codes, separated by commas. Here's one such sample:

C02,C11,U01,U02,D02,D32,D45,D67

These codes all mean something (pool, fenced in area, etc) and I have the meanings in a separate table. My question is, how should I handle loading these? Should I put them in their own field as they are in the CSV? Should I create a separate table that holds them?

If I do leave them as they are in a field (called feature_codes), how could I get the descriptions out of a table that has the descriptions? That table is simply feature_code, feature_code_description. I don't know how to break them apart in my first query to do the join to bring the description in.

Thank you

As a general rule, csv data should never stored in a field, especially if you actually need to consider individual bits of the csv data, instead of just the csv string as a whole.

You SHOULD normalize the design and split each of those sub "fields" into their own table.

That being said, MySQL does have find_in_set() which allows you sort-of search those csv strings and treat each as its own distinct datum. It's not particularly efficient to use this, but it does put a bandaid on the design.

You should keep the information about feature codes in a separate table, where each row is a pair of house identifier, and feature identifier

HouseID    FeatureID
1          C07
1          D67
2          D02

You can use explode() to separate your CSV string : http://php.net/manual/en/function.explode.php

$string = 'C02,C11,U01,U02,D02,D32,D45,D67';
$array  = explode(',', $string);

Then with your list of feature_codes you can easily retrieve your feature_code_description but you need to do another query to get an array with all your feature_codes and feature_code_description.

Or split your field and put it in another table with the home_id.

You can save it in your DB as is and when you read it out you can run the php function explode. Go check that function out. It will build an array for you out of a string separating the values by whatever you want . In your case you can use:

$array_of_codes = explode(",", $db_return_string);

This will make an array out of each code separating them by the commas between them. Good luck.