I have a product table which contains a field called 'categories' to save product related category ids as comma separated values. I am using regexp to search products from a category.
Assume there is record containing 4,24,1,31
my expression is,
..WHERE categories REGEXP ',?4,?'
but this returns both product of category 4 and 24
I just need to display only category 4.
Am I missing something?
解决方案
Use
WHERE categories REGEXP "(^|,)4(,|$)"
This matches 4 if surrounded by commas or at the start/end of the string.
In your present version, both commas are entirely optional, so the 4 in 24 matches.