0

三表课程,注册,学生

学生专栏

firstname,lastname,studentid,major,admitdate,graddate,gender,dob

注册栏

courseid,studentid

课程中的专栏

coursenumber,coursename,credits

我需要修改的select语句

select lastname as 'Last Name',sum(credits) as 'Credits Registered For' from students   as s
inner join registration as r on s.studentid = r.studentid
inner join courses as c on c.coursenumber = c.courseid
group by last name;

实验室的问题是... 修改前面的查询以显示所有学生,即使他们还没有注册课程。你应该有 14 行。未注册的学生将在输出中显示 NULL。

我知道这需要某种外部连接,但我没有完全掌握这些连接,我在这里和其他网站上阅读了多篇文章,但似乎无法弄清楚。

4

2 回答 2

3

利用LEFT JOIN

select  lastname as 'Last Name',
        sum(credits) as 'Credits Registered For' 
from    students   as s
        LEFT join registration as r on s.studentid = r.studentid
        LEFT join courses as c on c.coursenumber = r.courseid
group by last name;
于 2012-11-16T04:55:16.907 回答
0

尝试

SELECT  s.lastname AS 'Last Name',
        sum(c.credits) AS 'Credits Registered For' 
FROM    students    s
        LEFT JOIN registration  r ON (s.studentid = r.studentid)
        INNER JOIN courses  c ON (c.coursenumber = r.courseid)
GROUP BY lastname;

注意:AS给表名取别名时不需要使用

于 2012-11-16T04:58:05.177 回答