搜索“全字匹配”在MySQL中 [英] Search for "whole word match" in MySQL

查看:68
本文介绍了搜索“全字匹配”在MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个SQL查询来搜索文本字段中的关键字,但只有当它是全字匹配时(例如,当我搜索rid时,它不应该匹配arid,但它应该匹配摆脱。



我正在使用MySQL。

幸运的是,性能并不重要在这个应用程序中,数据库大小和字符串大小都很小,但我宁愿在SQL中执行它,而不是在PHP中执行它。 方案

您可以使用 REGEXP [[:<:]] [[:>:]] 字边界标记:

  SELECT * 
FROM表
WHERE关键字REGEXP'[[:<:]] rid [[:>]]'


I would like to write an SQL query that searches for a keyword in a text field, but only if it is a "whole word match" (e.g. when I search for "rid", it should not match "arid", but it should match "a rid".

I am using MySQL.

Fortunately, performance is not critical in this application, and the database size and string size are both comfortably small, but I would prefer to do it in the SQL than in the PHP driving it.

解决方案

You can use REGEXP and the [[:<:]] and [[:>:]] word-boundary markers:

SELECT *
FROM table 
WHERE keywords REGEXP '[[:<:]]rid[[:>:]]'

这篇关于搜索“全字匹配”在MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆