从 Json 数组中提取 JSON 对象到 mysql 8
Extract JSON Object from Json Array into mysql 8
["{"delivered":0,"deliveryDate":1633537686480,"toUserId":148}","{"delivered":0,"deliveryDate":1633537687590,"toUserId":226}", "{"delivered":1,"deliveryDate":1633537687741,"toUserId":160}","{"delivered":0,"deliveryDate":1633537687863,"toUserId":262}","{"delivered": 0,"deliveryDate":1633537688019,"toUserId":263}","{"delivered":0,"deliveryDate":1633537688174,"toUserId":264}","{"delivered":0,"deliveryDate": 1633537688325,"toUserId":265}"]
我想从下面提取这个 JSON 数组 table 然后我想进一步提取 JSON 对象,我在其中提供 userId 和 deliveryStatus 。我怎样才能实现这个?
预期结果
当我给 toUserid 148 并且还给了 deliveryStatus= 0
{{"交付":0,"交付日期":1633537686480,"toUserId":148}
您可以使用 JSON_TABLE
function to convert JSON into rows; then filter using ->
运算符:
SELECT t.id, JSON_PRETTY(j.obj)
FROM t
CROSS JOIN JSON_TABLE(t.delivery_status, '$[*]' COLUMNS(
obj JSON PATH '$'
)) AS j
WHERE j.obj->'$.delivered' = 0 AND j.obj->'$.toUserId' = 148
["{"delivered":0,"deliveryDate":1633537686480,"toUserId":148}","{"delivered":0,"deliveryDate":1633537687590,"toUserId":226}", "{"delivered":1,"deliveryDate":1633537687741,"toUserId":160}","{"delivered":0,"deliveryDate":1633537687863,"toUserId":262}","{"delivered": 0,"deliveryDate":1633537688019,"toUserId":263}","{"delivered":0,"deliveryDate":1633537688174,"toUserId":264}","{"delivered":0,"deliveryDate": 1633537688325,"toUserId":265}"]
我想从下面提取这个 JSON 数组 table 然后我想进一步提取 JSON 对象,我在其中提供 userId 和 deliveryStatus 。我怎样才能实现这个?
预期结果 当我给 toUserid 148 并且还给了 deliveryStatus= 0
{{"交付":0,"交付日期":1633537686480,"toUserId":148}
您可以使用 JSON_TABLE
function to convert JSON into rows; then filter using ->
运算符:
SELECT t.id, JSON_PRETTY(j.obj)
FROM t
CROSS JOIN JSON_TABLE(t.delivery_status, '$[*]' COLUMNS(
obj JSON PATH '$'
)) AS j
WHERE j.obj->'$.delivered' = 0 AND j.obj->'$.toUserId' = 148