我需要这个查询来选择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
答案 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, '%')