[英]Splitting json elements in an array using json_array in mySQL
我正在嘗試從 mySQL 數據庫表中檢索一些信息,其中我正在創建 json 類型的列。 在這里,我輸入了一個完整的 json 數組,該數組由單個 json 組成。
{"Date": "2019-09-30", "TableName": "es_apn_1", "Count": "3"}, {"Date": "2019-09-30", "TableName": "es_response_1", "Count": "297"}, {"Date": "2019-09-30", "TableName": "es_group_1", "Count": "356"}
所以我試圖使用 json_array() 和 json_object() 獲取單個 json,然后獲取它們各自的值。
我試過輸入 json 就像:
{"Date": "2019-09-30", "TableName": "es_apn_1", "Count": "3"}, {"Date": "2019-09-30", "TableName": "es_response_1", "Count": "297"}, {"Date": "2019-09-30", "TableName": "es_group_1", "Count": "356"}
或以數組形式,如:
[{"Date": "2019-09-30", "TableName": "es_apn_1", "Count": "3"}, {"Date": "2019-09-30", "TableName": "es_response_1", "Count": "297"}, {"Date": "2019-09-30", "TableName": "es_group_1", "Count": "356"}]
或在 json 中:
{{"Date": "2019-09-30", "TableName": "es_apn_1", "Count": "3"}, {"Date": "2019-09-30", "TableName": "es_response_1", "Count": "297"}, {"Date": "2019-09-30", "TableName": "es_group_1", "Count": "356"}}
我用來獲取結果的查詢是:
select json_object(js,'Date'), json_object(js,'TableName'), json_object(js,'Count') from (select json_array(jsondata) as js);
其中 jsondata 是存在上述條目的列的名稱。
但是我每次都以這種格式得到結果。
{"[\"{\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_apn_1\\\", \\\"Count\\\": \\\"3\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_response_1\\\", \\\"Count\\\": \\\"297\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_group_1\\\", \\\"Count\\\": \\\"356\\\"}\"]": "Date"} | {"[\"{\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_apn_1\\\", \\\"Count\\\": \\\"3\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_response_1\\\", \\\"Count\\\": \\\"297\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_group_1\\\", \\\"Count\\\": \\\"356\\\"}\"]": "TableName"} | {"[\"{\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_apn_1\\\", \\\"Count\\\": \\\"3\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_response_1\\\", \\\"Count\\\": \\\"297\\\"}, {\\\"Date\\\": \\\"2019-09-30\\\", \\\"TableName\\\": \\\"es_group_1\\\", \\\"Count\\\": \\\"356\\\"}\"]": "Count"}
您能幫我查詢或插入條目以正確獲得所需 output 的格式嗎?
您可以使用以下命令執行此操作:
CREATE TABLE tbl (id int auto_increment primary key, jdat json);
INSERT INTO tbl (jdat)
VALUES ('[{"Date": "2019-09-30", "TableName": "es_apn_1", "Count": "3"},
{"Date": "2019-09-30", "TableName": "es_response_1", "Count": "297"},
{"Date": "2019-09-30", "TableName": "es_group_1", "Count": "356"}]');
select JSON_EXTRACT(jdat,'$[0].Date') from tbl
在這里查看我的小演示: https://rextester.com/DPQ80237
正如@Raymond Nijland 所說的非常正確,存儲此類信息的推薦方法是在適當的表中,如下所示:
create table tbl2 (id int auto_increment primary key, refid int, tdate date, tname nvarchar(32), tcount int);
INSERT INTO tbl2 (refid, tdate, tname, tcount)
VALUES (1,'2019-09-30','es_apn_1',3),
(1,'2019-09-30','es_response_1',297),
(1,'2019-09-30','es_group_1',356);
通過這樣的設計,包含的數據更容易用於搜索或JOIN
與其他表的訪問......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.