How to search multiple columns in MySQL?(如何在 MySQL 中搜索多列?)
问题描述
我正在尝试制作一个搜索功能,该功能将搜索多个列以查找基于关键字的匹配项.此查询:
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 中搜索多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySQL 中搜索多列?
基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
