我已将以下数据插入列doc。
insert into tbl_temp (sid, doc)
values('M000001',
'{"doclist":
[{"PID":"F000001","name":"Roi","Age":"32","City":"OLI","Occ":"Reader"},
{"PID":"F000002","name":"Joi","Age":"33","City":"LND","Occ":"Runner"},
{"PID":"F000003","name":"Kloi","Age":"34","City":"NYC","Occ":"Writer"}
]
}'
)
现在我想显示所有的PID,所以我在mysql中进行了如下查询。
SELECT
SID,
json_extract(doc, '$.doclist[*].PID') AS PID
FROM MAT.TBL_temp;
但我得到的结果是这样的 -
SID Shortlisted
M000001 [" F000001"," F000002"," F000003"]
但我想要下面的结果
SID Shortlisted
M000001 "F000001"
M000001 "F000002"
M000001 "F000003"
也不确定我为什么会得到' []'在输出中。
当我运行以下查询(对于单个元素)时,它工作正常
SELECT
SID,
json_extract(doc, '$.doclist[0].PID') AS PID
FROM MAT.TBL_temp;
结果:
SID Shortlisted
M000001 "F000001"