Reputation: 1669
Basically I have products data stored in a database which also have a field called "tags" which is designed to store tags about that specific product. These tags are stored in a format like so:
"tag1,tag2,tag3,tag4"
The question is say that I want to have a page where a user can view all the latest products for that specific tag. How can I possibly accomplish this without taking every single product, fetching the "tag" field, separating them and then finally checking if that specific tag exists in there?
Is it possible to do this with a mysql command?
I am sorry I didnt post any code, I just am not sure how I should go about doing something like this in general.
Thank you!
Upvotes: 0
Views: 32
Reputation: 22817
You should normalize your database, so you can perform the search straight via SQL query.
Upvotes: 1
Reputation: 191749
Probably not the most efficient way, but:
WHERE tags LIKE '%$search_tag%'
This assumes that a tag that is searched for cannot be contained by another tag, or you'd need:
WHERE
tags LIKE '%,$search_tag%'
OR tags LIKE '%$search_tag,%'
OR tags = '$search_tag'
Obviously, search_tag
needs to be escaped properly.
Upvotes: 1