0

我正在处理一个 SQL 查询,我完全迷失在连接中。希望您能提供帮助:)

我的问题是来自单独表的行没有合并到它们都具有相同 ID 的单行中。很长一段时间以来,加入一直让我感到困惑,因此我们将不胜感激。

我有以下表格:

Students
id

StudentCredits
sum_credits

StudentMandatoryCredits
branch_credits
program_credits

我正在尝试创建一个查询,它将给我这样的行:

id         - sum_credits - branch_credits - program_credits
another_id - sum_credits - branch_credits - program_credits
a_third_id - sum_credits - branch_credits - program_credits

这是我能想到的最好的:

id - sum_credits - branch_credits -     [empty]
id - sum_credits -     [empty]    - program_credits

这是我的 SQL 语句:

SELECT S.id, SC.sum_credits, SMC.branch_credits, SMC.program_credits
FROM Students S
LEFT JOIN StudentCredits SC ON S.id = SC.id
LEFT JOIN StudentMandatoryCredits SMC ON S.id = SMC.id
ORDER BY id
4

1 回答 1

3

尝试使用:

SELECT S.id, SUM(SC.sum_credits), SUM(SMC.branch_credits), SUM(SMC.program_credits)
FROM Students S
LEFT JOIN StudentCredits SC ON S.id = SC.id
LEFT JOIN StudentMandatoryCredits SMC ON S.id = SMC.id
GROUP BY S.id
ORDER BY S.id
于 2012-11-22T22:00:30.590 回答