Reputation: 143
honestly i don't know the right title for my problem and i am still newbie about query things. So I have table name EVENT like this
id_event name_event category
----------------------------------------------
1 Market Food,Fashion,Art
2 Concert Music
3 FoodTruck Food,Bevarage
My problem is i want to select category where category = "Food,Fashion" . So All category which have "Food" and "Fashion" will be out and the result be like this
id_event name_event category
----------------------------------------------
1 Market Food,Fashion,Art
3 FoodTruck Food,Bevarage
Maybe someone can help me, Thank You Guys and Have a nice day!! Cheerss!!
Upvotes: 0
Views: 53
Reputation: 13519
This way you can achieve what you want:
SELECT
*
FROM event
WHERE category REGEXP CONCAT('(^|,)(', REPLACE("Food,Fashion", ',', '|'), ')(,|$)');
Note: If you want to see the generated regular expression in the above query:
SELECT CONCAT('(^|,)(', REPLACE("Food,Fashion", ',', '|'), ')(,|$)') AS 'REGEXP';
Result:
(^|,)(Food|Fashion)(,|$)
Explanation: Records will be selected having Food/fashion
in category
name only if anyone of the following condition is met:
food/fashion
has a comma before it orcategory
starts with food/fashion
orcategory
ends with food/fashion
or
food/fashion
has a comma after it.
Suggestion:
Is storing a delimited list in a database column really that bad?
Upvotes: 1