连接查询 left join on, union

1.自然连接

连接s和sc表

select s.sno,sn,sex,age,dept,cno,score from s ,sc where s.sno=sc.sno

2.外连接

左连接 left join on 连接s和sc表

select s.sno,sn,sex,age,dept,cno,score from s left  join sc on s.sno=sc.sno

s表为主体,即使s6没有选课,信息也会出现在查询结果中

3.合并查询 union

从sc数据表中查询出学号为s1的同学的学号和总分,再从sc数据表中查询出学号为s2的同学的学好和学分,然后将两个查询结果合并成一个结果表。

select sno as 学号,sum(score)as 总分 from sc where sno='s1' group by sno
union
select sno as 学号,sum(score)as 总分 from sc where sno='s2' group by sno

union 会将重复的行剔除,合并的子查询的表结构和数据数目要相同

union all 不会将重复行删除

数据库表s,c,sc截图请到:http://www.cnblogs.com/fuge/archive/2012/03/16/2400913.html

原文地址:https://www.cnblogs.com/fuge/p/2342570.html