2 u010535345 u010535345 于 2016.03.14 11:37 提问

mysql5.7如何获取json数组里的某些值 1C

json数组是这样的:
[
{"name":"a","score":20,"wrongID":[1,2,6,7]},
{"name":"b","score":50,"wrongID":[1,2,3,7]},
{"name":"c","score":40,"wrongID":[1,2,9,17]},
{"name":"d","score":70,"wrongID":[1,2,16,27]},
{"name":"e","score":80,"wrongID":[1,2,10,17]},
{"name":"f","score":50,"wrongID":[1,2,10,17,22,33]},
]

表名student,列名是examInfo
比如,我要获取数组里score为50的wrongID,要怎么获取呢??
我之前写了一条sql:
select examInfo->'$[*].wrongID' from student where examInfo->'$[*].score'=50;
结果查不出来,没数据也没报错,50变成了'50',结果也是一样,没数据。
有大牛知道怎么查吗?请教!!

6个回答

Royal_lr
Royal_lr   Ds   Rxr 2016.03.14 11:41

这是php?,,还有就是你写sql从json中查数据?

u010535345
u010535345 就是写sql
一年多之前 回复
u010535345
u010535345 就是写生气了、
一年多之前 回复
u010535345
u010535345   2016.03.17 00:20

自顶啊!!!!求解!!!!!!!!

liumuchengquan
liumuchengquan   2016.12.06 14:46

这个sql的问题在于$[*].score的返回值是一个json数组,这个返回值是无论如何不会等于50或者‘50’的。顺便问一句最后你怎么查出来的?

ggop1355555
ggop1355555   2017.03.29 17:49

我也求解,要是解决请大神帖出来,多谢

mingyu1016
mingyu1016 不好意思,刚大意了,上面写的不对,如下这样写: SELECT t1.rowobj -> '$.wrongID' FROM ( SELECT examInfo->'$[0][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[1][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[2][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[3][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[4][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[5][0]' AS rowobj FROM student ) t1 where t1.rowobj -> '$.score' = 50
6 个月之前 回复
mingyu1016
mingyu1016 不好意思,刚大意了,上面写的不对,如下这样写: SELECT t1.rowobj -> '$.wrongID' FROM ( SELECT examInfo->'$[0][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[1][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[2][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[3][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[4][0]' AS rowobj FROM student UNION ALL SELECT examInfo->'$[5][0]' AS rowobj FROM student ) t1 where t1.rowobj -> '$.score' = 50
6 个月之前 回复
mingyu1016
mingyu1016 select examInfo->'$[*].wrongID' from student where examInfo->'$[*].score[0]'=50;
6 个月之前 回复
mingyu1016
mingyu1016   2017.06.27 17:35

大牛来了,这样写select examInfo->'$[*].wrongID' from student where examInfo->'$[*].score[0]'=50;

mingyu1016
mingyu1016   2017.06.27 18:24

不好意思,刚大意了,上面写的不对,如下这样写:
SELECT t1.rowobj -> '$.wrongID'
FROM (
SELECT examInfo->'$[0][0]' AS rowobj FROM student
UNION ALL
SELECT examInfo->'$[1][0]' AS rowobj FROM student
UNION ALL
SELECT examInfo->'$[2][0]' AS rowobj FROM student
UNION ALL
SELECT examInfo->'$[3][0]' AS rowobj FROM student
UNION ALL
SELECT examInfo->'$[4][0]' AS rowobj FROM student
UNION ALL
SELECT examInfo->'$[5][0]' AS rowobj FROM student
) t1

where t1.rowobj -> '$.score' = 50

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!