Possible Duplicate:
Mysql WHERE problem with comma-separated list
I have the next info in a table
name categories
------ -----------
John 1,4
Jim 4,1
JAck 4,1
between other.
I want to select all the rows that pertain to category 4, but this statement..
SELECT name, categories
FROM `mytable`
WHERE 4 IN (categories)
returns only “Jim” and “Jack” but not “John”. What am I doing wrong?
Edit: Sadly I cant change the structure of the table. I need to use that comma-list style
I am not sure why you are storing a comma separated list in a single column. But it you cannot change the table structure, you will want to use
LIKEto compare the data:See SQL Fiddle with Demo
Or you can use the
FIND_IN_SETfunction:See SQL Fiddle with Demo
My suggestion would be to change your table to not store your data in this comma-separated format, it will cause nothing but problems.