如何在MySQL中搜索多个列? [英] How to search multiple columns in MySQL?

查看:85
本文介绍了如何在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中搜索多个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆