有帮助吗?

解决方案

如果您想要返回包含任意关键字的结果,请将所有 AND 替换为 OR

如果您这样做,那么您的数据库将检查表中任何列中是否存在任何关键字。

因此,您的最终查询将由DB服务器读取,如下所示:

SELECT * FROM blog WHERE blog_title LIKE '%barry%' OR blog_content LIKE '%barry%' OR blog_title LIKE '%child%' OR blog_content LIKE '%child%' OR blog_title LIKE '%help%' OR blog_content LIKE '%help%' ORDER BY blog_title

并且应该返回在任何列(blog_title, blog_content)中包含关键字(barry,child,help)的所有记录。

希望这有帮助。

其他提示

AND运算符的优先级高于OR,因此如果要返回包含所有关键字的结果,则需要添加括号,如下所示:

  SELECT * FROM blog
  WHERE (blog_title LIKE '%barry%' OR blog_content LIKE '%barry%')
    AND (blog_title LIKE '%child%' OR blog_content LIKE '%child%')
    AND (blog_title LIKE '%help%' OR blog_content LIKE '%help%')
  ORDER BY blog_title
scroll top