使用REGEXP进行MYSQL查询 - make-it非贪心

时间:2016-01-06 15:00:21

标签: mysql regex

我需要一些关于MYSQL查询的正则表达式的帮助来搜索包含具有精确模式的单元格的行。我是MYSQL正则表达式的新手。

这是一个名为test_table的示例表(json_value是数组的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"}

如果我想获取所有行:

- 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行的field_9

另一个问题是(我知道,这是一个愚蠢的正则表达式)

SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^quality]*quality.*';

但仅返回id 1

我在互联网上发了很多帖子但没有成功。 正则表达式如何获得正确的行?

修改 还有一个想法,更明确的是,第一个来自搜索的工作是来自数组的完整键,但第二个是部分值,如"%substring%"

谢谢

1 个答案:

答案 0 :(得分:2)

这是我能想到的最好的:

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行,但它不是。但是如果你有这样的数据,我认为你别无选择,只能真正解析它,并且正则表达式不会成功。