开发者

Searching multiple categories separated by commas in MySQL TEXT field

So heres what i'm trying to do but can't figure out. A site i'm doing allows a person to select several categories to be in like 'Boxing','Weight Loss','Endurance' etc etc... These are all seperated by commas and sit in one TEXT field in the members table.

Now what im trying to do is find out how I can search multiple categories at once (Like say endurance and boxing) and find all members who have either / both of these in their category field.

I thought of something like开发者_如何转开发 profile_specialising IN ('Cat1','Cat2','Cat3')... but that doesn't work when the profile_specialising field has more than 1 category/field in the db.

So I can search with multiple categories in a field with multiple categories.


I really suggest you put categories in a separate table and relate them to members with a third table categories_to_members. But if you want to keep things as they are a hypothetical query would look like this.

SELECT * FROM members WHERE category LIKE '%cat%' OR category LIKE '%dog%'

There are inherent problems with this -- LIKE '%cat%' would match "catatonic" as well as "cat." Trying to solve this by adding the commas to the LIKEs is more trouble than it's worth, IMHO.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜