MySQL-可以在表的所有列上使用LIKE吗? [英] MySQL - Is it possible to use LIKE on all columns in a table?
问题描述
我正在尝试创建一个简单的搜索栏,该搜索栏可以在数据库中搜索某些单词.是否可以在不使用WHERE的情况下使用LIKE属性?我希望它在所有列中搜索关键字,而不仅仅是一个.目前我有这个:
I'm trying to make a simple search bar that searches through my database for certain words. It is possible to use the LIKE attribute without using WHERE? I want it to search all columns for the keywords, not just one. Currently I have this:
mysql_query("SELECT * FROM shoutbox WHERE name LIKE '%$search%' ")
显然,仅使用搜索输入来搜索名称.我尝试了这两种方法:
Which obviously only searches for names with the search input. I tried both of these:
mysql_query("SELECT * FROM shoutbox LIKE '%$search%' ")
mysql_query("SELECT * FROM shoutbox WHERE * LIKE '%$search%' ")
都没有用.这是可能的,还是还有另一种解决方法?
and neither worked. Is this something that is possible or is there another way to go about it?
推荐答案
您可能还想看看MATCH()函数,例如:
You might want to look at the MATCH() function as well eg:
SELECT * FROM shoutbox
WHERE MATCH(`name`, `foo`, `bar`) AGAINST ('$search')
您还可以为此添加布尔模式:
You can also add boolean mode to this:
SELECT * FROM shoutbox
WHERE MATCH(`name`, `foo`, `bar`) AGAINST ('$search') IN BOOLEAN MODE
您还可以获取相关性得分并添加FULLTEXT键以加快查询速度.
You can also get the relevance scores and add FULLTEXT keys to speed up the queries.
这篇关于MySQL-可以在表的所有列上使用LIKE吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!