从JSON中选择数据

时间:2018-06-18 03:54:41

标签: sql sql-server

目前我仍然坚持如何从JSON中选择一个值。

广告资源

Name   | Json_Column
-------+--------------------------------------------------------------------
Ball   | [{"skuId":"1","quantity":1,"skuName":"$5 valley ball"},{"skuId":"2","quantity":1,"skuName":"$10 BasketBall"}]
Racket | [{"skuId":"3","quantity":1,"skuName":"$5 Badminton racket"}]

我想使用select SQL语句将以下结果转换为:

Name   | Json_Column
-------+---------------------------------------
Ball   | $5 valley ball, $10 BasketBall
Racket | $5 Badminton racket

当我使用此SQL语句进行选择时,它返回null;我需要一些SQL帮助。

select JSON_VALUE (Json_Column, '$.skuName') as Name 
from Inventory

1 个答案:

答案 0 :(得分:1)

尝试使用OPENJSON

SELECT * FROM OPENJSON(Json_Column, '$') 
WITH (skuId int'$.skuId',quantity int '$.quantity',skuName varchar(50) '$.skuName')

<强>更新

好的,所以从表中选择你可以用OPENJSON交叉申请

SELECT skuName from Inventory
CROSS APPLY
OPENJSON (Json_Column)
WITH (skuId int'$.skuId',quantity int '$.quantity',skuName varchar(50) '$.skuName')