1

I have a database and some of the columns contain things like CA, GB etc, although some contain multiple country codes like

US+GB+CA+AU

I'm just wondering what kind of query I would do that would return that row when I'm searching for just CA or just GB, and no necessarily the whole package US+GB+CA+AU

Encase that's a little confusing, basically I just need to return that row based on a search for just CA or just GB etc.

Thanks

3
  • 3
    Why do some of your columns contain multiple data? I'd advise you to look at that and try and normalise it. It will cause you no end of problems... Commented Mar 21, 2012 at 12:27
  • it just the way the data was being stored in a csv before i imported it, i may very well just normalise it. Commented Mar 21, 2012 at 14:39
  • In that case I would normalise your schema now. It's easy enough to split the concatenated strings in the csv before you import the data. Commented Mar 21, 2012 at 14:42

3 Answers 3

4

Use FIND_IN_SET(), but you'll first need to replace + with , since it expects a comma-separated string. Even without the REPLACE(), this is will not make use of any index on the countrycodes column.

SELECT * FROM tbl 
WHERE FIND_IN_SET('AU', REPLACE(countrycodes, '+', ',')) > 0

The proper long term solution, however, is to change your database structure to normalize these country codes into a table that contains only two columns - a country code, and the id of the associated row from the table you're attempting to query now. You can then index the column appropriately to improve performance (possibly drastically improve it).

Sign up to request clarification or add additional context in comments.

2 Comments

I believe according to the MySQL doc, the correct syntax for REPLACE would be REPLACE(countrycodes, '+', ',').
@Travesty3 Yes, you're correct thanks. I just did a string replacement in a different language with params in the other order...
3

I would recommend to normalise it like liquorvicar said.

but using SELECT ... WHERE countrycode LIKE '%GB%' would work. http://w3schools.com/sql/sql_like.asp

Comments

2

It's not a good solution, but you can use LIKE for your query:

SELECT * FROM `table` WHERE `field` LIKE '%+CA+%' OR `field` LIKE 'CA+%' OR `field` LIKE '%+CA' OR `field` = 'CA'

Last two checks for firs and last values.

5 Comments

that's not necessary. % is a substitute for zero or more characters
@CarrieKendall but we have LIKE '%+CA+%' not LIKE '%CA%'. First value is not +
@CarrieKendall: I think the idea is to be extra safe in case you get a value like CCA, which you don't want to be returned.
but those ONLY return the records that have multiple country codes. Read OP's first sentence.
@CarrieKendall: A good point. You should add OR field = 'CA' to the WHERE clause. @MarcusAdams: Agreed, which is why I was referring to it as "extra safe" :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.