的一個節點我有這個JSON在我的數據庫:PostgreSQL的 - NoSQL的查詢思路選擇JSON
{
"Id": 1,
"Questions": [
{
"QuestionId": 6,
"Description": "Question 1",
"Alternatives": [
{
"Index": 1,
"CorrectAnswer": false,
"AlternativeId": 26,
"QuestionId": 6,
"Description": "Alternative one",
"Selected": false
},
{
"Index": 2,
"CorrectAnswer": true,
"AlternativeId": 27,
"QuestionId": 6,
"Description": "Alternative two",
"Selected": false
}
]
},
{
"QuestionId": 7,
"Description": "Question 2",
"Alternatives": [
{
"Index": 1,
"CorrectAnswer": false,
"AlternativeId": 26,
"QuestionId": 6,
"Description": "Alternative one",
"Selected": false
},
{
"Index": 2,
"CorrectAnswer": true,
"AlternativeId": 27,
"QuestionId": 6,
"Description": "Alternative two",
"Selected": false
}
]
}
]
}
我不能得到這個文件只是一個問題。我試過以下查詢:
select data#>'{Questions}' from db.my_table
where (data #> '{Questions,0,QuestionId}')::numeric = 6;
SELECT data ->> 'Questions' AS Questions FROM db.my_table
WHERE (data -> 'Questions' ->> 'QuestionId')::numeric = 6;
SELECT data ->> 'Questions' AS Questions FROM db.my_table
WHERE data -> 'Questions' ->> 'QuestionId' = '6'
我能做什麼錯?我總是得到0受影響的行的回報。
文檔:
https://www.postgresql.org/docs/current/static/datatype-json.html https://www.postgresql.org/docs/current/static/functions-json.html
每一個幫助是值得歡迎!
嘿@jmelesky感謝您的迴應!但我有以下錯誤: '錯誤:函數json_array_elements(jsonb)不存在 線1:從db.m選擇json_array_elements(數據 - >'問題')... ^ 提示:沒有函數匹配給定名稱和參數類型。您可能需要添加顯式類型轉換。 **********錯誤**********'當我運行最後一個查詢(笛卡爾)。我的專欄是jsonb ... – IgoR
如果您的專欄是jsonb,而不是json,那麼您應該使用jsonb功能而不是json功能。正如[documentation](https://www.postgresql.org/docs/current/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE)所示,這意味着使用'jsonb_array_elements',而不是'json_array_elements '。將來,在你的問題中提供關於你的模式的更多信息將會有所幫助。 – jmelesky
!非常感謝你,現在的作品......我會提供一些有關我的模式的信息,以便你能看到一個可能的「最佳解決方案」。 :) – IgoR