For example, I have php array $php_arr_to_check = array(\"green\", \"red\", \"blue\");
And have many mysql rows like this
Id | TextToCheck
I think you want to use Boolean Full-Text Search
If you match without operators +
-
against such as green red blue
all rows are returned, where a record contains at least one word: green
or red
or blue
.
IN BOOLEAN MODE
and without operators each matched word will score 1
. So if there's a record matching two out of the three words it would score 2
.
To get the rows with at least 2 score:
SELECT *,
MATCH (`TextToCheckIn`) AGAINST ('green red blue' IN BOOLEAN MODE) `score`
FROM `my_tab` WHERE
MATCH (`TextToCheckIn`) AGAINST ('green red blue' IN BOOLEAN MODE)
HAVING `score` >= 2
ORDER BY `score` DESC
In Natural Language mode scoring works completely different. Think it's primarily based on BM25.
On large datasets boolean fulltext search (using a fulltext index) usually outperforms REGEXP
or LIKE
by far if matching words somewhere in the text. Would only use like/regexp for matching from the initial such as REGEXP '^word'
or LIKE 'word%'
- if an index can be utilized.
I haven't MySQL for to test next query. You try something:
SELECT Id, TextToCheckIn REGEXP "(${word1})|(${word2})" AS Flag
FROM table
WHERE Flag = 1