I use MySql REGEXP:
SELECT * FROM myTable
WHERE title REGEXP "dog|cat|mouse";
The dataset is small, so I am not concerned about performance. And I prefer this over LIKE notation, because I do not have to concatenate a bunch of "LIKE" statements.
However, the above notation uses a logical OR operator. Is there a logical AND operator, so that only rows containing all of the keywords are matched?
(I am using InnoDB so fulltext search not an option)
You can add several conditions with
AND
between them:Maybe REGEXP is not necessary here and you may use INSTR instead (regular are usually slower):
There's really no nice solution except concatenating ANDs:
The regular expression would otherwise look like this:
AND
operation may be only accessible by the mysql-AND:this will only show those entries where all the keywords are in the title field. like
You can use full text search in boolean mode:
You must index your table first:
If your title contains all the search terms, for example:
Anything mouse anything cat anything dog anything
Then you will have all the search terms in a three times repeated title in any order (for example dog, cat and mouse).
Anything mouse anything cat anything DOG anything
Anything mouse anything CAT anything dog anything
Anything MOUSE anything cat anything dog anything
So you can do this without concatenating ANDs with:
SELECT * FROM myTable WHERE REPEAT(title,3) RLIKE '.*dog.*cat.*mouse.*';