I have a list of comma separated tags in a column in my database. I was using a simple wildcard to pull items that matches a tag that I was searching for. However, I have discovered that some tags are included as part of a title for other tags and that they are being returned also. For example, searching for hunting these rows might be returned:
hunting, fishing, outdoors <-- Should be returned
sporting goods, hunting <-- Should be returned
bargain hunting, sale <-- Should NOT be returned
bullets, guns, hunting <-- Should be returned
Currently I am using:
WHERE column LIKE '%hunting%'
What should I do to make this work more appropriately with these comma seperated lists. Also, please bear in mind that some rows may have only one tag and hence have no commas at all. Thanks.
Use
RLIKE
with the appropriate regex:See an SQLFiddle live demo of this condition working correctly with the sample input from the question.