mysql: instr 指定单词边界 [英] mysql: instr specify word boundaries
问题描述
我想检查一个字符串是否包含一个字段值作为子字符串.
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屋!