Mysql匹配column1或column2中的所有单词

时间:2010-11-04 09:27:50

标签: regex mysql

例如:在MySql中我有column1和column2。 如何进行sql查询,返回包含第1列或第2列中所有搜索词的所有记录。 到目前为止我一直在使用:

Select * from table1 where (column1 like '%search from input box%' or column2 like '%search from input box%')

1 个答案:

答案 0 :(得分:0)

如果您的表格为MyISAM

SELECT  *
FROM    table1
WHERE   MATCH(column1) AGAINST ('+search +from +input +box' IN BOOLEAN MODE)
UNION
SELECT  *
FROM    table2
WHERE   MATCH(column2) AGAINST ('+search +from +input +box' IN BOOLEAN MODE)

FULLTEXT INDEXcolumn1(独立)上创建column2,以便快速开展工作,并调整@ft_min_word_len以匹配您需要的字词。

如果不是MyISAM

SELECT  *
FROM    table
WHERE   (
        column1 RLIKE '[[:<:]]search[[:>:]]'
        AND column1 RLIKE '[[:<:]]from[[:>:]]'
        AND column1 RLIKE '[[:<:]]input [[:>:]]'
        AND column1 RLIKE '[[:<:]]box[[:>:]]'
        )
        OR
        (
        column2 RLIKE '[[:<:]]search[[:>:]]'
        AND column2 RLIKE '[[:<:]]from[[:>:]]'
        AND column2 RLIKE '[[:<:]]input [[:>:]]'
        AND column2 RLIKE '[[:<:]]box[[:>:]]'
        )