SQL选择2个LIKE

时间:2012-08-22 13:36:31

标签: mysql sql-like

我需要这个查询来选择category = logotypes的所有帖子,而full_text就像它已经一样喜欢搜索词。

SELECT      *
FROM        posts
WHERE       category = :logotypes
AND         full_text LIKE CONCAT('%', :term, '%')
ORDER BY    post_date DESC

但我希望搜索字词还检查是否在标题中完成了匹配:

    SELECT      *
    FROM        posts
    WHERE       category = :logotypes
    AND         full_text LIKE CONCAT('%', :term, '%')
--> ALSO CHECK  heading LIKE CONCAT('%', :term, '%')
    ORDER BY    post_date DESC

我该怎么办?

P.S该类别应始终保留。它必须仍为category = :logotypes

2 个答案:

答案 0 :(得分:4)

只需将ALSO CHECK替换为OR

SELECT      *
FROM        posts
WHERE       category = :logotypes
AND        (full_text LIKE CONCAT('%', :term, '%')
OR          heading LIKE CONCAT('%', :term, '%'))
ORDER BY    post_date DESC

观察OR - 连接谓词

周围的括号

答案 1 :(得分:1)

SELECT      * 
FROM        posts 
WHERE       category = :logotypes 
AND        
(
    full_text LIKE CONCAT('%', :term, '%') 
    OR 
    heading LIKE CONCAT('%', :term, '%')  
)
ORDER BY    post_date DESC 

或者,如果你感觉模糊不清

AND CONCAT(full_text,'/', heading) LIKE CONCAT('%', :term, '%')