MySQL-如何使用LIKE搜索完全匹配的单词? [英] MySQL - How to search for exact word match using LIKE?
本文介绍了MySQL-如何使用LIKE搜索完全匹配的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此查询来选择数据:
I'm using this query to select data:
mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'");
唯一的问题是,有时选择的余地比我想要的还要多.
The only problem is, that it sometimes selects more, than I would like.
例如,我想选择产品"BLA",但我的查询也选择了产品"BLABLA".要明确的是,如果我想选择产品1",则不希望查询选择产品11".
For example, I would like to select product "BLA", but my query select product "BLABLA" as well. To be clear, if i wanted to select "Product 1", I don't want the query to select "Product 11".
有人知道如何管理吗?
谢谢.
推荐答案
您是否只想搜索单词边界?如果是这样,则粗略版本可能是:
Do you just want to search on word boundaries? If so a crude version might be:
SELECT * FROM products WHERE product_name LIKE "% foo %";
或者您可能会更聪明,并使用以下REGEXP
Or you could be a bit cleverer and look for word boundaries with the following REGEXP
SELECT * FROM products WHERE product_name RLIKE "[[:<:]]foo[[:>:]]";
这篇关于MySQL-如何使用LIKE搜索完全匹配的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文