How to set the 'where' clause on a field with comma-seperated values?
mysql, select, sql
Solution
use `FIND_IN_SET()`
SELECT *
FROM tableName
WHERE FIND_IN_SET('value3', 'comma separated value here') > 0
- SQLFiddle Demo
SOURCE
- MySQL FIND_IN_SET
Description from MySQL Docs:
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL.
Problem
I have a db table with a field with values stored in the `value1,value2,value3,value4` format. I want to find all the rows where this field contains a defined value, eg. `value3`. How can I perform a query to search a value in a field like this one?