我的数据库中有一串信息,它是一串 json,我正试图从中获取一些特定的信息。下面是一个 json 示例,我试图从每个条目中获取 SuID
和 Quantity
。有时只有一个条目,有时有两个以上的条目,但它们总是采用这样的格式。
我尝试使用 charindex
、left
、right
、substring
和 replace
,但是当我需要多个部分时,我不知道如何让它工作。
[{
"SuID": 8348,
"SuParentSuID": 652,
"Quantity": 0,
"LeadTime": 72,
"LeadTimeClosures": 0,
"Transships": 0,
"InventoryStatus": 2,
"HasCurrentFeed": 1,
"LeadTimeSource": 0,
"GhcPrgBitwise": 0,
"QuantityAdjustment": 0,
"ExclusionCriteria": 0
}, {
"SuID": 8349,
"SuParentSuID": 652,
"Quantity": 454,
"LeadTime": 72,
"LeadTimeClosures": 0,
"Transships": 0,
"InventoryStatus": 1,
"HasCurrentFeed": 1,
"LeadTimeSource": 1,
"GhcPrgBitwise": 0,
"QuantityAdjustment": 0,
"ExclusionCriteria": 0
}]
我需要得到像这张表这样的东西:
Suid | Quantity
------|----------
8348 | 0
8349 | 454
最佳答案
使用 Postgres,您可以使用实际的 json
数据类型来使这个非常简单:
SELECT j->>'Suid' AS suid
, j->>'Quantity' AS quantity
FROM json_array_elements(my_json) j
my_json
是 json
类型的 JSON 值。
文字输入也会非常简单:
SELECT j->>'Suid' AS suid
, j->>'Quantity' AS quantity
FROM json_array_elements( <b>'[{
"SuID": 8348,
"Quantity": 0,
...
}, {
"SuID": 8349,
"Quantity": 454,
...
}]'::json</b>) j
关于sql - 如何在字符串中动态确定的位置选择动态数量的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894508/