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 中搜索多列?


- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01