4
我需要一些MYSQL查詢正則表達式的幫助來搜索包含具有精確模式的單元格的行。我是MYSQL正則表達式的新手。使用REGEXP進行MYSQL查詢 - make-it非貪婪
這是命名爲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
我希望的例子是concludent。
我曾嘗試使用此查詢
SELECT id from test_table WHERE json_value REGEXP 'field_4":".*quality.*';
但field_9第2行
另一個查詢返回id爲1,2和4,因爲是貪婪,找到了「質量」(我知道,這是一個愚蠢的正則表達式)
SELECT id from test_table WHERE json_value REGEXP 'field_4":"[^quality]*quality.*';
但僅返回ID 1
我已經readed很多在互聯網上的帖子,但沒有成功。 正則表達式應該如何得到正確的行?
編輯 還有一認爲,要更加明確,從搜索的第一部作品是從陣列中的全鍵,但第二部分的值,如「%字符串%」
謝謝
如果你是在MySQL 5.7看看JSON_CONTAINS – Mihai
我在Cpanel與Mysql 5.6.28 – vasilenicusor