Search string in comma separated list

Issue

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?

Solution

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.

Answered By – AllInOne

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published