I have a table of strings and want to retrieve all rows which contain at least two characters of a predefined list. Better explained by example:
My table looks like this:
Id|Word
1|Cat
2|Chicken
3|Dog
4|Elephant
5|Fish
6|Goat
This could be my char list:
Characterlist = 'A', 'B', 'C', 'E'
The select statement should then return:
Cat, Chicken, Elephant
I have looked through the string functions in mySQL docs but can not find a suitable function for doing this. Is it even possible?
Here’s an inefficient way which relies on adding up the 0 or 1s returned by boolean conditions:
The
LIKEconditions each return a 0 or 1. Those are added together, and theHAVINGclause limits the result to the rows scoring 2 or greater.It might be faster to use
LOCATE()instead ofLIKE: