mysql: instr 指定单词边界 [英] mysql: instr specify word boundaries

查看:34
本文介绍了mysql: instr 指定单词边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个字符串是否包含一个字段值作为子字符串.

select * from mytable where instr("mystring", column_name);

但这不会搜索单词边界.

select * from mytable where instr("mystring", concat('[[:<:]]',column_name,'[[:>:]]');

也不起作用.如何纠正?

解决方案

您可以使用 REGEXP 运算符:

SELECT * FROM mytable WHERE 'mystring' REGEXP CONCAT('[[:<:]]', column_name, '[[:>:]]');

但是请注意,这.您最好使用 MySQL 的 FULLTEXT 搜索功能 如果您关心单词.或者做一个普通的 InStr() 检查然后过滤结果.

i want to check if a string contains a field value as a substring or not.

select * from mytable where instr("mystring", column_name);

but this does not search on word boundaries.

select * from mytable where instr("mystring", concat('[[:<:]]',column_name,'[[:>:]]');

does not work either. how to correct this?

解决方案

You can do this using the REGEXP operator:

SELECT * FROM mytable WHERE 'mystring' REGEXP CONCAT('[[:<:]]', column_name, '[[:>:]]');

Note, however, that this is slow. You might be best off using the MySQL's FULLTEXT search feature if you care about words. Or do a normal InStr() check then filter the results.

这篇关于mysql: instr 指定单词边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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