gpt4 book ai didi

sql - 如何在字符串中动态确定的位置选择动态数量的子字符串?

转载 作者:行者123 更新时间:2023-11-29 13:58:21 24 4
gpt4 key购买 nike

我的数据库中有一串信息,它是一串 json,我正试图从中获取一些特定的信息。下面是一个 json 示例,我试图从每个条目中获取 SuIDQuantity。有时只有一个条目,有时有两个以上的条目,但它们总是采用这样的格式。

我尝试使用 charindexleftrightsubstringreplace,但是当我需要多个部分时,我不知道如何让它工作。

[{
"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_jsonjson 类型的 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

JSON Functions and operators in the manual.

关于sql - 如何在字符串中动态确定的位置选择动态数量的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com