简体   繁体   English

PHP MySQL:搜索查询仅适用于现有的完整术语

[英]PHP MySQL: Search query work only with existing full term

I got a search query fully work but only if i use the correct term (ie 'iPhone 6 Plus' but if i search only 'iPhone' it will return nothing. 我得到了一个完全有效的搜索查询,但只有当我使用正确的术语(即'iPhone 6 Plus'但如果我只搜索'iPhone'时它将不会返回任何内容。

My search query is: SELECT * FROM products WHERE name LIKE '" . $term . "' OR brand LIKE '" . $term . "' LIMIT 0 , 30" 我的搜索查询是: SELECT * FROM products WHERE name LIKE '" . $term . "' OR brand LIKE '" . $term . "' LIMIT 0 , 30"

So how I can create a better query that can search better? 那么我如何创建一个更好的查询,可以更好地搜索?

Thanks a Lot to all who can help. 非常感谢所有能提供帮助的人。

In order to do matches on partial search terms you have to add some wildcards to your queries, the % : 要在部分搜索字词上进行匹配,您必须在查询中添加一些通配符%

SELECT * FROM products WHERE name COLLATE UTF8_GENERAL_CI LIKE '%" . $term . "%' OR brand COLLATE UTF8_GENERAL_CI LIKE '%" . $term . "%' LIMIT 0 , 30"

You could just add COLLATE UTF8_GENERAL_CI to your column definitions and then you would not have to modify your queries. 您只需将COLLATE UTF8_GENERAL_CI添加到列定义中,然后就不必修改查询。 For example: 例如:

ALTER TABLE products MODIFY COLUMN name VARCHAR(...) CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI. 

If you need something more complex you will want to look a fulltext searches . 如果您需要更复杂的东西,您将需要查看全文搜索

The Latin collation ( LATIN1_GENERAL_CS )is one of those which is known to work well with case insensitive searches. 拉丁语排序规则( LATIN1_GENERAL_CS )是已知适用于不区分大小写搜索的其中之一。 If the one I specified (I try to always use UTF-8) doesn't work, substitute the Latin collation. 如果我指定的那个(我尝试总是使用UTF-8)不起作用,则替换拉丁语排序规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM