I have a table with a column named tags
. In this column, I have a comma-separated list like air conditioner, tv, fridge
.
I want to check if any of the words in the comma-separated column exists in a string like
Is there an air conditioner in the room?
So, the query should return a match as in the column tags
, the air conditioner
value exists.
I was thinking of using find_in_set
, but it will not work in this case as the logic should be reversed.
select * from products where find_in_set('Is there an air conditioner in the room?',tags) <> 0
UPDATE
I could use find_in_set
by separating the string into words and then checking against the tags column, but it will not work for a combination like "air conditioner" as it will spit like air, conditioner
.
3
Answers
You can test it like this:
fiddle
Or remove always all spaces. Then you can find it with or without spaces Like:
fiddle
Demo: https://dbfiddle.uk/ayNuWEqQ
This cannot be optimized with an index, just like any other substring-matching task.
Don’t use a comma-separated field in the first place. Normalize your schema by putting the tags into their own table with a foreign key pointing back to the
products
table.Then you can use