我正在对 MySQL 数据库进行一些研究,其中一些数据以 XML 格式存储。我已经设法找到了我要搜索的字符串:
select * from foo where Concat(foo) like '%bar%';
现在我试图只查找“bar”出现 2 次的条目。在我正在搜索的表格中,“bar”总是出现一次,所以我想找到至少有 2 倍“bar”的条目。
你能给我一些建议吗?
最佳答案
你应该使用REGEXP
方法
select * from foo where Concat(foo) regexp '(bar).*(bar)';
分解
()
- 第一个捕获组
bar
- 要捕获的表达式
.
- 匹配任何字符
*
- 匹配零个或多个字符
(bar)
- 第二个捕获组
https://regex101.com/r/wM3wX9/1
来自MySQL文档
Performs a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression, the syntax for which is discussed later in this section. Returns 1 if expr matches pat; otherwise it returns 0. If either expr or pat is NULL, the result is NULL. RLIKE is a synonym for REGEXP, provided for mSQL compatibility.
我还为此创建了一个 SQL Fiddle。
关于mysql - 在 MySQL 中查找字符串时遇到一些麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429961/