MySQL - 如何使用 LIKE 搜索精确的单词匹配?

MySQL - How to search for exact word match using LIKE?(MySQL - 如何使用 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 搜索精确的单词匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)