且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Oracle中的JOIN

更新时间:2022-06-24 08:03:09

一、两张表

Student表

SID                  SNAME                CLASSID

-------------------- -------------------- --------------------
001                  小明                 1
002                  小红                 2
003                  小张                 4

Class表
ID                   NAME
-------------------- --------------------
1                    一班
2                    二班
3                    三班


二、自连接
只返回两张表连接列的匹配项。以下三种效果相同
select * from student s inner join class c on s.classid = c.id;
select * from student s join class c on s.classid = c.id;
select * from student s,class c where s.classid = c.id;

SID                  SNAME                CLASSID              ID                   NAME
-------------------- -------------------- -------------------- -------------------- --------------
001                  小明                 1                    1                    一班
002                  小红                 2                    2                    二班

 

三、笛卡儿乘积连接
即不加任何条件,达到 M*N 的结果集。以下两种查询结果一样。
select * from student s cross join class c;
select * from student,class;

SID                  SNAME                CLASSID              ID                   NAME
-------------------- -------------------- -------------------- -------------------- --------------
001                  小明                 1                    1                    一班
001                  小明                 1                    2                    二班
001                  小明                 1                    3                    三班
002                  小红                 2                    1                    一班
002                  小红                 2                    2                    二班
002                  小红                 2                    3                    三班
003                  小张                 4                    1                    一班
003                  小张                 4                    2                    二班
003                  小张                 4                    3                    三班

除了cross join不可以加on外,其它join连接都必须加上on关键字,后都可加where条件。加上条件,产生跟自连接一样的结果。
select * from student s cross join class c where s.classid = c.id;


四、左连接
列出左边表全部的,及右边表符合条件的,不符合条件的以空值代替。在(+)计算时,哪个带(+)哪个需要条件符合的,另一个全部的。即放左即右连接,放右即左连接。以下结果集相同。

select * from student s left join class c on s.classid = c.id;
select * from student s,class c where s.classid = c.id(+);

SID                  SNAME                CLASSID              ID                   NAME
-------------------- -------------------- -------------------- -------------------- --------------
001                  小明                 1                    1                    一班
002                  小红                 2                    2                    二班
003                  小张                 4


五、右连接
与左连接一样,列出右边表全部的,及左边表符合条件的,不符合条件的用空值替代。(+)一样,它的位置与连接相反。
select * from student s right join class c on s.classid = c.id;
select * from student s,class c where s.classid(+) = c.id;

SID                  SNAME                CLASSID              ID                   NAME
-------------------- -------------------- -------------------- -------------------- --------------
001                  小明                 1                    1                    一班
002                  小红                 2                    2                    二班
                                                                      3                    三班  

六、全连接
产生M+N的结果集,列出两表全部的,不符合条件的,以空值代替。
select * from student s full join class c on s.classid = c.id;

SID                  SNAME                CLASSID              ID                   NAME
-------------------- -------------------- -------------------- -------------------- --------------
001                  小明                 1                    1                    一班
002                  小红                 2                    2                    二班
003                  小张                 4                                        

                                                                      3                    三班

七、总结

(+)连接符只能Oracl中应用,join等语句其他数据库也适用。


原帖地址:http://www.cnblogs.com/lovemoon714/archive/2012/03/02/2376782.html