在字符串上的零件上查询mysql



名称列包含'John Smith','Elsa John Sanders',Liljohn Thomson','John'之类的项目。我该如何构建查询只是为了与约翰返回名字,但不能返回liljohn。

我不能做类似的"%john%",因为它会返回" Liljohn Thomson"。

看起来与:搜索"整个单词匹配"在mysql

该方法使用光滑的正则表达式。

假设一个单词是由空间定义的,您可以做:

where concat(' ', col, ' ') like '% John %'

以下表达式应找到所有" john" s

a = 'John' OR
a LIKE 'John %' OR
a LIKE '% John %' OR
a LIKE '% John'

请注意,其他一些技巧也有效,但可能会严重命中性能,例如

CONCAT(" ",a," ") LIKE "% John %"

使用 空间通配符尝试类似于'% john %'。

您可以使用 REGEXP 函数。

尝试以下操作:

SELECT * 
FROM tableA a 
WHERE a.name REGEXP '[[:<:]]John[[:>:]]'

最新更新