MySQL Select exact word
-
07-12-2019 - |
Question
I currently have database items with "Skönhet Kroppsvård Massage Spa" as category (in one string).
Currently I use
WHERE Select6 LIKE '%spa%'
What I want is to select eg. "Spa" and get the fields containing "Spa" and not "Spas"
Solution
Look into mySQL fulltext search. It takes some time to get into and has some caveats, but it's probably the only sane way to fulfill your requirement.
Boolean full-text searches should work for you, for example
SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('+Spa' IN BOOLEAN MODE);
OTHER TIPS
Try following condition, one checks for word spa anywhere in text and other at end.
WHERE Select6 LIKE '% spa %' or Select6 LIKE '% spa' or Select6 LIKE 'spa' or Select6 LIKE 'spa %'
Don't put the '%' at the end. That symbol is equivalent to saying "any string here". So when you do %spa%, it will match any string that contains spa somewhere in the middle.
If all your strings are formatted as you said and your search term is always at the end, just do '%spa'. This way it will find you strings that end with "Spa".
So, for example: WHERE Select6 LIKE '%Spa'
Remove % at the end of position. Because both side % search "Spa" in word not end of position.
where Select6 LIKE '%Spa
I like @rs. way because if you forgot the trim, you have that rows too, so use this way
where Select6 LIKE '%Spa %' or Select6 LIKE '%Spa'
You could try
WHERE Select6 LIKE '%spa%' AND NOT LIKE '%spas%'
this will take xxxxspaxxxx but not xxxxspasxxxx
So it will work if spa is not the last word or the first one
Or
WHERE Select6 LIKE '% spa %' OR Select6 LIKE '% spa' OR Select6 LIKE 'spa %' OR Select6 LIKE 'spa' OR
spa at the end '% spa'
spa at the begining 'spa %'
only spa 'spa'