2016-09-08 44 views
1

我在MySQL 5.7表數據類型json和以下值的列:MySQL的:通過JSON屬性搜索返回錯誤

{"1": "test"} 
{"2": [25,23], "3": [28,54], "71": "test"} 
{"2": [25,12], "3": [28,72], "33": "test2"} 
{"2": [25,11], "3": [28,23], "63": "test3"} 
{"4": "test4"} 

我需要查詢與屬性的所有的行「3」(不管值) ,所以我在做這個查詢:

SELECT * FROM `notes` WHERE JSON_CONTAINS(data, '{"2"}'); 

我越來越:

#3141 - Invalid JSON text in argument 2 to function json_contains: "Missing a colon after a name of object member." at position 5. 

也試過:

SELECT data->"$.2" AS myattr FROM `notes`; 
#3143 - Invalid JSON path expression. The error is around character position 3. 

請幫忙。

回答

1

使用JSON_CONTAINS_PATH因爲你不關心價值

SELECT data 
    FROM notes 
WHERE JSON_CONTAINS_PATH(data, 'one', '$."3"'); 

輸出示例:

 
+-----------------------------------------------+ 
| data           | 
+-----------------------------------------------+ 
| {"2": [25, 23], "3": [28, 54], "71": "test"} | 
| {"2": [25, 12], "3": [28, 72], "33": "test2"} | 
| {"2": [25, 11], "3": [28, 23], "63": "test3"} | 
+-----------------------------------------------+