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 Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01