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

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()检查筛选结果。

其他提示

如果您不需要instr使用like的返回值,而不是

select * from mytable where column_name like '%mystring%';

正如您在问题中已经讨论的那样 昨天问, ,不能使用索引,性能会很差,但这可以工作:

select *
from mytable
where 'mystring' = column_name                           -- exact match
   or 'mystring' like concat('% ', column_name)          -- word at the end
   or 'mystring' like concat(column_name, ' %')          -- word at beginning
   or 'mystring' like concat('% ', column_name, ' %')    -- word in the middle
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top