Retrieve rows where a value exists within a comma separated list

Posted by Klaaz on Stack Overflow See other posts from Stack Overflow or by Klaaz
Published on 2012-06-21T08:38:47Z Indexed on 2012/06/21 9:16 UTC
Read the original article Hit count: 187

Filed under:

I like to reverse this query:

SELECT * FROM table WHERE status IN ( option,option1,option2 );

in a query like

SELECT * FROM table WHERE status contains ( 'option' );

Where field 'status' column contains (by example): 'option,option1'.

When I use

SELECT * FROM table WHERE status LIKE '%option%' );

It also selects the rows with a 'status' column that only contains 'option1'

Is this possible?

example data:

id,name,status
1,'test1','option,option1'
2,'test2','option,option2'
3,'test3','option2'
4,'test4','option2,option3'
5,'test5','option'

SELECT * FROM table WHERE status contains ( 'option' );

This query should select select record 1,2 and 5.

'status' field type is varchar.

The problem is that the status fields contains data that is almost alike, exept from the added number to option. Off course this is a made up example but the real thing has this kind of parameters

© Stack Overflow or respective owner

Related posts about mysql