搜索“全字匹配”在MySQL中 [英] Search for "whole word match" in 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屋!