我有这个json字符串
[{"count": 9, "name": "fixkit", "label": "Repair Kit"}, {"count": 1, "name": "phone", "label": "Telefoon"}]
[{"count": 3, "name": "phone", "label": "Telefoon"}]
[{"count": 5, "name": "kunststof", "label": "Kunststof"}, {"count": 6, "name": "papier", "label": "Papier"}, {"count": 2, "name": "metaal", "label": "Metaal"}, {"count": 2, "name": "inkt", "label": "Inkt"}, {"count": 3, "name": "kabels", "label": "Kabels"}, {"count": 2, "name": "klei", "label": "Klei"}, {"count": 2, "name": "glas", "label": "Glas"}, {"count": 12, "name": "phone", "label": "Telefoon"}]
[{"count": 77, "name": "weed", "label": "Cannabis"}, {"count": 1, "name": "firework1", "label": "Vuurpijl 1"}]
我想得到以下结果
Phone | Number of phones (in this case: 16)
Fixkit | Number of fixkits (in this case: 9)
我想用sql查询来实现这一点。如果你知道怎么做,请提前感谢!
2条答案
按热度按时间6jjcrrmo1#
如果您不使用mysql 8,这就有点复杂了。首先你要找到一条通往
name
具有值的元素phone
(或fixkit
); 然后你可以替换name
在那条路上count
并提取count
从那条路来的田地;这些值可以相加:输出:
在dbfiddle上演示
pobjuy322#
如果您运行的是mysql 8.0,那么可以使用
json_table()
,然后在name
你感兴趣的是什么。假设你的table是
mytable
json列被称为js
,即:db小提琴演示: