使用REGEXP的MYSQL查询-使其不贪婪
我需要一些关于MYSQL查询正则表达式的帮助,以搜索包含具有精确模式的单元格的行。我是MYSQL正则表达式的新手 这是一个名为test_table的示例表(json_值是数组的json字符串) 如果要使用以下命令获取所有行:使用REGEXP的MYSQL查询-使其不贪婪,mysql,regex,Mysql,Regex,我需要一些关于MYSQL查询正则表达式的帮助,以搜索包含具有精确模式的单元格的行。我是MYSQL正则表达式的新手 这是一个名为test_table的示例表(json_值是数组的json字符串) 如果要使用以下命令获取所有行: - field_4 containing "quality" string, the query should to return id 1 and 4 - field_9 containing "quality" string, the query should to r
- field_4 containing "quality" string, the query should to return id 1 and 4
- field_9 containing "quality" string, the query should to return id 1 and 2
- field_4 containing "ordering" string, the query should to return id 2 and 3
我希望这个例子是结论
我已尝试使用此查询
SELECT id from test_table WHERE json_value REGEXP 'field_4":".*quality.*';
但是返回id 1、2和4,因为它是贪婪的,并在第2行的字段_9处找到“质量”
另一个问题是(我知道,这是一个愚蠢的正则表达式)
但只返回id 1
我在网上读了很多帖子,但都没有成功。
正则表达式应该如何获得正确的行
编辑
再想一想,更明确地说,搜索的第一个工作是数组中的完整键,但第二个是部分值,如“%substring%”
谢谢这是我能想到的最好的办法:
SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^"]*quality';
鉴于您提供的数据,它符合您的要求。但是,如果您的JSON包含嵌入/转义双引号,则会中断,如:
+----+----------------------------------------------------------------------------------------------------------------+
| id | json_value |
+----+----------------------------------------------------------------------------------------------------------------+
| 1 | {"field_198":false,"field_4":"From quality office","field_9":"product with high quality","field_10":"comment"} |
| 2 | {"field_198":true,"field_4":"From ordering office","field_9":"back to quality office","field_10":"comment"} |
| 3 | {"field_198":true,"field_4":"From ordering office","field_9":"cancelled","field_10":"comment"} |
| 4 | {"field_198":true,"field_4":"Return to quality office","field_9":"product ok","field_10":"comment"} |
| 5 | {"field_198":true,"field_4":"Return to \"quality\" office","field_9":"product ok","field_10":"comment"} |
+----+----------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^"]*quality';
+----+
| id |
+----+
| 1 |
| 4 |
+----+
2 rows in set (0.01 sec)
在这种情况下,我想您应该希望返回id=5的行,但事实并非如此。但是如果你有这样的数据,我想你别无选择,只能真正解析它,而正则表达式不会起作用。如果你使用的是mysql 5.7,请查看JSON_contains我使用的是带有mysql 5.6.28的Cpanel
SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^"]*quality';
+----+----------------------------------------------------------------------------------------------------------------+
| id | json_value |
+----+----------------------------------------------------------------------------------------------------------------+
| 1 | {"field_198":false,"field_4":"From quality office","field_9":"product with high quality","field_10":"comment"} |
| 2 | {"field_198":true,"field_4":"From ordering office","field_9":"back to quality office","field_10":"comment"} |
| 3 | {"field_198":true,"field_4":"From ordering office","field_9":"cancelled","field_10":"comment"} |
| 4 | {"field_198":true,"field_4":"Return to quality office","field_9":"product ok","field_10":"comment"} |
| 5 | {"field_198":true,"field_4":"Return to \"quality\" office","field_9":"product ok","field_10":"comment"} |
+----+----------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^"]*quality';
+----+
| id |
+----+
| 1 |
| 4 |
+----+
2 rows in set (0.01 sec)