I have this query:
SELECT * FROM mytable
WHERE column1 LIKE '%word1%'
AND column1 LIKE '%word2%'
AND column1 LIKE '%word3%'
I need to modify this query to return records for which column1 contains word1 word2 and word3 and nothing else! no other words, just these words.
Example: searching for samsung galaxy s3 should return any combination of samsung s3 galaxy but NOT samsung galaxy s3 lte
Assuming that
column1contains space separated words, and you only want to match on whole words, something like:Note that this construction does allow the same word to appear multiple times. It’s not clear from the question whether that should be allowed. (Fiddle)
It would be a far better design if these words were stored as separate rows in a separate table that relates back to
mytable. We could then use more normal SQL to satisfy this query. Your example looks like it’s some kind of tagging example. Having a table storing each tag as a separate row (with an ordinal position also recorded, if required) would turn this into a simple relational division problem.A way to count how many times a word appears in a column is the expression:
but this would again revert back to matching subsequences of larger words as well as the word itself, without more work.