MySQL-从字符串中搜索确切的单词 [英] MySQL - Search exact word from string
本文介绍了MySQL-从字符串中搜索确切的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从像这样的字符串中搜索确切的单词
I want to search exact word from string like
id Description
1 This is nice pen looking good
2 This is nice pendrive looking good
搜索字符串:pen
我当前的查询
SELECT * FROM `table` WHERE Description like '%pen%';
以上查询返回两条记录,但我只希望第一条记录.因为 pen 单词与我的搜索字符串完全匹配.
Above Query return both record but I want Only first record. Because pen word exact match with my search string.
预期产量
1 This is nice pen looking good
Demo
推荐答案
尝试使用正则表达式:
SELECT
*
FROM
`table`
WHERE
Description regexp '(^|[[:space:]])pen([[:space:]]|$)';
演示
或使用字边界:
Demo
Or using word boundaries:
SELECT
*
FROM
`table`
WHERE
Description regexp '[[:<:]]pen[[:>:]]';
这篇关于MySQL-从字符串中搜索确切的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文