Data stored in the database as below where category is column name and comma separated strings are values.
category = 'cat,cat1,cat2,cat3,cat33,cat4';
I'm trying to search with a category from the list:
$query= mysqli_query($mysqli, "SELECT * FROM table where
INSTR(category,'cat3') > 1 ");
But this doesn't work because the result includes cat3 and cat33.
Any thoughts on how I can do this search, please?
Thanks!
"SELECT * FROM table where category LIKE '%cat3,%'";
"SELECT * FROM table where category LIKE '%cat33,%'";
if you don't want to add comma to the end of each category name, then "SELECT * FROM table where category LIKE '%cat3,%' OR category LIKE '%cat3'";
You can use FIND_IN_SET function :
SELECT * FROM table where FIND_IN_SET('cat3', category) > 0
But you should normalize your data by creating category table and relate to your table's primary key
You can "solve" this using the query below. BUT I HARDLY RECCOMEND you to normalize your database and use this only if you REALLY NEED
SELECT * FROM table WHERE fild LIKE '%,camp3,%' OR fild LIKE 'camp3' OR field LIKE '%,camp3'