I agree with the comments about re-designing the database. At first read it seems that using LIKE
would definitely get the result you want but after reading @Patrick Q's pan - panther example, it makes a lot sense that LIKE
is not really a good solution. There are ways to get exactly the tag string you're looking for but it may hurt the performance and the query will be longer and complex. Hence the following are to demonstrate how the query would look like with your current tags
data value:
MySQL query:
SELECT tags,
SUBSTRING_INDEX(SUBSTRING_INDEX(tags,' ',FIND_IN_SET('usb',REPLACE(tags,' ',','))),' ',-1) v
FROM mytable
HAVING v = 'usb';
As you can see, there are a few functions being used just to get the exact string from the data cell. Since your example data was separating with spaces and FIND_IN_SET
identify value separation by comma, REPLACE
take place on the tags
column first to replace spaces with comma. Then with SUBSTRING_INDEX
twice to get the string using the location extracted in FIND_IN_SET
. Finally at the end HAVING
to get only the tag you're looking for.
Further demo here : https://www.db-fiddle.com/f/joDa7MNcQL2RakTgBa7qBM/3