简体   繁体   中英

Search string in comma separated list

I'm trying to search a table of recipes that DON'T include any of the search terms in it's ingredients. The ingredients of each recipe are stored in one field, separated by commas.

I've tried using WHERE NOT MATCH...AGAINST but that doesn't work in cases like when I search for Peas. It will return results with pea protein in them. I need to not have that. This is also a problem with pluralization.

I tried using NOT REGEXP but I'm not too familiar with regular expressions and all the Googling I've done for the last two days hasn't produced a result.

Is there ANY way I can search this field in this table for multiple values and exclude them in the results?

Check out the operators for the MATCH... AGAINST...

I think what you are looking for is:

WHERE MATCH(ingredients) AGAINST ("-pea*" in boolean mode)

Examples:

pea protein,radish
// not returned (as desired)

peas,beans
// not returned (as desired)

But there may still be issues:

peanut butter,bacon
// not returned (not as desired)

green pea soup,bacon
// returned (not as desired)

I'm sort of feeling like to do this 'right' you would need meta data on each ingredient .

Or alternatively you have allergen 'tags' that you apply to recipes -- separate from the ingredients themselves.

For instance if someone is allergic to 'gluten' that's not actually an ingredient at the recipe level. It's a property of many recipe ingredients tho...

This approach would require you to compile a list of all known allergens and then apply them to the recipes, not sure of your context but allergen tagging could work as a folksonomy task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM