带有空格的 MySQL 搜索字符串使用 LIKE [英] MySQL Search String with Spaces Using LIKE
问题描述
我正在我的网站上构建一个搜索,但我注意到当您在搜索中输入多个单词时它不起作用.这是查询的要点:
I'm building a search on my site and I noticed it doesn't work when you enter more than one word into the search. Here's the gist of the query:
SELECT * FROM `blog` WHERE `content` LIKE '%$keyword%' OR `title` LIKE '%$keyword%' ORDER BY `id` DESC
奇怪的是,当我在 phpMyAdmin 中测试查询时,它返回了预期的结果.然而,在我的网站上,没有找到任何结果.
The weird things is that when I test the query in phpMyAdmin it returns the expected results. On my website however, no results are found.
我尝试用 %s 替换关键字中的空格,但没有任何改变.
I tried replacing spaces in the keyword with %s, but that didn't change anything.
推荐答案
问题在于 LIKE
执行模式匹配而不是实际搜索关键字.您应该在数据库列上创建全文索引并使用 WHERE MATCH 关键字 AGAINST 列
.这将按任何顺序正确搜索所有关键字,并且速度更快.
The problem is that LIKE
does pattern matching rather than actually search for keywords. You should create a fulltext Index on your database columns and use WHERE MATCH keywords AGAINST column
. That will properly search for all keywords in any order and be a lot faster anyway.
这篇关于带有空格的 MySQL 搜索字符串使用 LIKE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!