如何使用嵌套子查询SQL获取嵌套关系实体?
How to get nested relationship entities with nested subqueries SQL?
我有这 4 个表格:记分卡、部分、主题、答案。
每个记分卡可以有多个部分。每个部分可以有很多主题,每个主题可以有很多答案。
我希望以下列形式检索数据。获取记分卡 id 等于某个 id 的所有部分,我通过左连接来做到这一点,到目前为止它一直有效。然后对于每个部分,我也想获得它的所有主题和答案。
=> 预期输出
[{section1,
topics:[{topicName, answers:[{answerName}]}]},
{section2,
topics:[{topicName, answers:[{answerName}]}]}]
我调整了我的查询并正确地连接了表格,但输出数据没有像我原来那样正确组织。
查询:
SELECT sec."id",
sec."name" AS "sectionName",
to_json(topics) as "topics"
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.*, to_json(answers.*) as "answers"
FROM "topic" tp
LEFT JOIN (SELECT ans."name" as "answerName", ans."topicId"
FROM "answer" ans) answers ON answers."topicId" = tp."id"
) topics
ON topics."sectionId" = sec."id"
WHERE sc."id" =
当前输出:
[ id: 85,
sectionName: 'Consultation',
topics: {
id: 109,
name: 'Was the information correct?',
answers: [{ answerName: 'Yes', topicId: 109 }, answers: { answerName: 'NO', topicId: 109 }
]
},
id: 85,
sectionName: 'Consultation',
topics: {
id: 109,
name: 'Was the information correct?',
answers: { answerName: 'NO', topicId: 109 }
}]
我的预期输出:
[ {id: 85,
sectionName: 'Consultation',
topics: [{
id: 109,
name: 'Was the information correct?',
answers: [{ answerName: 'Yes', topicId: 109 },
{ answerName: 'NO', topicId: 109 }]
}]
} ]
我也尝试加入所有实体:
SELECT sec."id",
sec."name" AS "sectionName",
tp."name" AS topics, ans."name" AS answers
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN "topic" tp ON tp."sectionId" = sec."id"
LEFT JOIN "answer" ans ON ans."topicId"= tp."id"
WHERE sc."id" =
输出没有按应有的方式组织。
[
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'No'
},
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'Not everytime'
},
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'Yes'
}
]
您缺少 table topics
的 JOIN
条件
SELECT sec."id",
sec."name" AS "sectionName",
topics
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.*
FROM "topic" tp
WHERE tp."sectionId" = sec."id"
(SELECT tp.*
FROM "topic" tp
WHERE tp."sectionId" = sec."id"
)
) as topics ON topics.??? = ??? -- Here you should enter the relation
-- from topics to your section.
WHERE sc."id" = 30
编辑:
根据你的(当前)输入,我创建了这个 DBFIDDLE
输出:
[
{
"id": 85,
"sectionName": "Consulation",
"topics": {
"id": 109,
"name": "Was the information correct?",
"sectionId": 85,
"answers": {
"answerName": "Yes",
"topicId": 109
}
}
},
{
"id": 85,
"sectionName": "Consulation",
"topics": {
"id": 109,
"name": "Was the information correct?",
"sectionId": 85,
"answers": {
"answerName": "NO",
"topicId": 109
}
}
}
]
您可以连接所有四个表以获得结果。
SELECT sec."id",
sec."name" AS "sectionName",
tp."name" AS topics, ans."name" AS answers
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN "topic" tp on tp."sectionId" = sec."id"
LEFT JOIN "answer" ans on ans."topicId"=tp."id"
WHERE sc."id" = 30
感谢@Luuk 创建数据库fiddle link.
select json_agg(x) AS result
from
(
SELECT sec."id",
sec."name" AS "sectionName",
json_agg(topics) as "topics"
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.id,tp.name,tp."sectionId", json_agg(answers.*) as "answers"
FROM "topic" tp
LEFT JOIN (SELECT ans."name" as "answerName", ans."topicId"
FROM "answer" ans) answers ON answers."topicId" = tp."id"
group by tp.id,tp.name,tp."sectionId"
) topics
ON topics."sectionId" = sec."id"
WHERE sc."id" = 85
group by sec."id", sec."name"
)x
输出:
result
[{"id":85,"sectionName":"Consulation","topics":[{"id":109,"name":"Was the information correct?","sectionId":85,"answers":[{"answerName":"Yes","topicId":109}, <br> {"answerName":"NO","topicId":109}]}]}]
*db<>fiddle here149)
我有这 4 个表格:记分卡、部分、主题、答案。
每个记分卡可以有多个部分。每个部分可以有很多主题,每个主题可以有很多答案。
我希望以下列形式检索数据。获取记分卡 id 等于某个 id 的所有部分,我通过左连接来做到这一点,到目前为止它一直有效。然后对于每个部分,我也想获得它的所有主题和答案。
=> 预期输出
[{section1,
topics:[{topicName, answers:[{answerName}]}]},
{section2,
topics:[{topicName, answers:[{answerName}]}]}]
我调整了我的查询并正确地连接了表格,但输出数据没有像我原来那样正确组织。
查询:
SELECT sec."id",
sec."name" AS "sectionName",
to_json(topics) as "topics"
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.*, to_json(answers.*) as "answers"
FROM "topic" tp
LEFT JOIN (SELECT ans."name" as "answerName", ans."topicId"
FROM "answer" ans) answers ON answers."topicId" = tp."id"
) topics
ON topics."sectionId" = sec."id"
WHERE sc."id" =
当前输出:
[ id: 85,
sectionName: 'Consultation',
topics: {
id: 109,
name: 'Was the information correct?',
answers: [{ answerName: 'Yes', topicId: 109 }, answers: { answerName: 'NO', topicId: 109 }
]
},
id: 85,
sectionName: 'Consultation',
topics: {
id: 109,
name: 'Was the information correct?',
answers: { answerName: 'NO', topicId: 109 }
}]
我的预期输出:
[ {id: 85,
sectionName: 'Consultation',
topics: [{
id: 109,
name: 'Was the information correct?',
answers: [{ answerName: 'Yes', topicId: 109 },
{ answerName: 'NO', topicId: 109 }]
}]
} ]
我也尝试加入所有实体:
SELECT sec."id",
sec."name" AS "sectionName",
tp."name" AS topics, ans."name" AS answers
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN "topic" tp ON tp."sectionId" = sec."id"
LEFT JOIN "answer" ans ON ans."topicId"= tp."id"
WHERE sc."id" =
输出没有按应有的方式组织。
[
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'No'
},
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'Not everytime'
},
{
id: 91,
sectionName: 'Politeness',
topics: 'Was the agent polite with the client?',
answers: 'Yes'
}
]
您缺少 table topics
JOIN
条件
SELECT sec."id",
sec."name" AS "sectionName",
topics
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.*
FROM "topic" tp
WHERE tp."sectionId" = sec."id"
(SELECT tp.*
FROM "topic" tp
WHERE tp."sectionId" = sec."id"
)
) as topics ON topics.??? = ??? -- Here you should enter the relation
-- from topics to your section.
WHERE sc."id" = 30
编辑:
根据你的(当前)输入,我创建了这个 DBFIDDLE
输出:
[
{
"id": 85,
"sectionName": "Consulation",
"topics": {
"id": 109,
"name": "Was the information correct?",
"sectionId": 85,
"answers": {
"answerName": "Yes",
"topicId": 109
}
}
},
{
"id": 85,
"sectionName": "Consulation",
"topics": {
"id": 109,
"name": "Was the information correct?",
"sectionId": 85,
"answers": {
"answerName": "NO",
"topicId": 109
}
}
}
]
您可以连接所有四个表以获得结果。
SELECT sec."id",
sec."name" AS "sectionName",
tp."name" AS topics, ans."name" AS answers
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN "topic" tp on tp."sectionId" = sec."id"
LEFT JOIN "answer" ans on ans."topicId"=tp."id"
WHERE sc."id" = 30
感谢@Luuk 创建数据库fiddle link.
select json_agg(x) AS result
from
(
SELECT sec."id",
sec."name" AS "sectionName",
json_agg(topics) as "topics"
FROM "scorecard" sc
LEFT JOIN "section" sec ON sc."id" = sec."scorecardId"
LEFT JOIN (SELECT tp.id,tp.name,tp."sectionId", json_agg(answers.*) as "answers"
FROM "topic" tp
LEFT JOIN (SELECT ans."name" as "answerName", ans."topicId"
FROM "answer" ans) answers ON answers."topicId" = tp."id"
group by tp.id,tp.name,tp."sectionId"
) topics
ON topics."sectionId" = sec."id"
WHERE sc."id" = 85
group by sec."id", sec."name"
)x
输出:
result |
---|
[{"id":85,"sectionName":"Consulation","topics":[{"id":109,"name":"Was the information correct?","sectionId":85,"answers":[{"answerName":"Yes","topicId":109}, <br> {"answerName":"NO","topicId":109}]}]}] |
*db<>fiddle here149)