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.
Update:
Finally found it after Gumbo’s suggestion.
I first tried Gumbo’s lead (added the > 0):
SELECT * FROM table
WHERE FIND_IN_SET('option', status) > 0;
But it did not work either.
After some fiddeling around I found out that the culprit were the spaces between the options in the status field. These records are imported from another source and came with spaces after the comma:
‘option, option1’ instead of ‘option,option1’
So I ended up with:
SELECT * FROM table
WHERE FIND_IN_SET('option', TRIM(REPLACE(status,' ',''))) > 0;
This worked for me.
For comma separated values, you can use the
FIND_IN_SETfunction to search for a value in it:To negate the expression, use
NOTor!: