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 搜索精确的单词匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL - 如何使用 LIKE 搜索精确的单词匹配?
基础教程推荐
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
