检查mysql中出现2次的字符

Cla*_*ong 1 regex mysql sql string where-clause

我试图检查 o 是否在字符串中出现两次,我这样做了,但它不起作用,我的代码中是否有错误?

SELECT cu.*
FROM Customers cu
WHERE LOWER(cu.FirstName) REGEXP 'o{2}'

-- 'zoom' => correct
-- 'antonio' => correct
-- 'leo jisoo' => fail
Run Code Online (Sandbox Code Playgroud)

谢谢

GMB*_*GMB 5

您对两个连续的os 进行编码检查,这不是您想要的。

您可以将其写为:

where 
    FirstName like '%o%o%'
    and FirstName not like '%o%o%o%'
Run Code Online (Sandbox Code Playgroud)

这检查字符串是否恰好包含 to os。

另一种方法是:

length(replace(FirstName, 'o', '')) = length(FirstName) - 2
Run Code Online (Sandbox Code Playgroud)