php/mysql. Is there mysql function to detect if at least 2 certain words exists in at least one mysql row-field

前端 未结 2 1047
予麋鹿
予麋鹿 2021-01-24 04:13

For example, I have php array $php_arr_to_check = array(\"green\", \"red\", \"blue\");

And have many mysql rows like this

Id  |  TextToCheck         


        
相关标签:
2条回答
  • 2021-01-24 04:57

    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.

    0 讨论(0)
  • 2021-01-24 05:00

    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
    
    0 讨论(0)
提交回复
热议问题