I am trying to match keywords using multiple LIKE
‘s, however I would like to be able to do something like this:
SELECT * FROM `keywords` WHERE keyword LIKE '%pp%' AND keyword LIKE '%p%';
to match "hippopotamus" but not "hippo".
I understand I can simply do:
SELECT * FROM `keywords` WHERE keyword LIKE '%pp%p%';
However I was wondering if there are any other approaches I could take. The above works fine for this example but for other examples Ill have several OR
‘s such as:
LIKE '%pp%p%' OR LIKE '%p%pp%' OR LIKE '%pp%p%pp%
etc, and it will get very messy.
Does anyone know of a prettier way of doing this?
2
Answers
This can be achieved with
REGEXP
, like so:Tests and explanation: (MySQL Workbench 8.0 CE)
I’d suggest using MySQL REGEXP here, smth like this:
haven’t tested, but it should be true only if
p
andpp
are occured at the same time in any order