Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql WHERE SET datatype contains item

Tags:

mysql

I have a table that uses a SET datatype for one of the fields, and to check if the field contains a specific element I use

SELECT * FROM table WHERE myset LIKE %value%;

This works most of the time, but two of the potential values have the same word, i.e. one possible element in the set is Poodle and another is Toy Poodle. If I do

SELECT * FROM table WHERE myset LIKE %Poodle%;

It returns all rows that have either Poodle or Toy Poodle. I want it to only return if the field contains Poodle. If I remove the wildcards then it will only return the rows that have ONLY Poodle. So basically, if the table was:

id | myset
-------------------------
1  | "Poodle"
2  | "Toy Poodle"
3  | "Poodle","Toy Poodle"
4  | "Toy Poodle","Poodle"

I need a select statement that would return 1,3, and 4 but not 2. Is this possible?

like image 904
awestover89 Avatar asked Mar 17 '11 22:03

awestover89


1 Answers

You need to do: SELECT * FROM table WHERE FIND_IN_SET('Poodle',myset)>0 as described in documentation

like image 76
Laimoncijus Avatar answered Sep 28 '22 02:09

Laimoncijus