问题描述
我正在尝试制作一个搜索功能,该功能将搜索多个列以查找基于关键字的匹配项.此查询:
I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
仅适用于搜索一列,我注意到用逗号分隔列名会导致错误.那么可以在mysql中搜索多列吗?
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
推荐答案
您可以使用 AND 或 OR 运算符,具体取决于您希望搜索返回的内容.
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
两个子句都必须匹配才能返回记录.或者:
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
如果任何一个子句匹配,那么记录将被返回.
If either clause matches then the record will be returned.
有关您可以使用 MySQL SELECT 查询执行的操作的更多信息,请尝试使用 文档.
For more about what you can do with MySQL SELECT queries, try the documentation.
这篇关于如何在 MySQL 中搜索多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!