关于内连接、外连接、笛卡尔积

 
 内连接 (inner   join)    
内连接的特性是只显示符合连接条件的记录。
 通过比较源表间共享的列的值从多个源表检索行的操作。内联接排除来自不具有与其它源表中的行匹配的行的源表的行。
用join

外连接(outer join)

而外连接就不一样,以左外连接为例(右外连接类似),它除了显示符合连接条件的记录以外,还会显示所有左表中的记录(右外连接就是所有右表中的记录)。

 左连接即左外连接,显示左边表的所有行,用left join
 右连接即右外连接,显示右边表的所有行,用right join

全连接,返回左边表与右边表的所有记录,没有对应的用NULL表示, 用full join

 

关于笛卡尔积、全连接、union的测试

create table test111 (id number,name varchar2(20));

create table test222 (id number,name varchar2(20));

insert into test111 values(111,'aaa');

insert into test111 values(222,'bbb');

insert into test111 values(444,'ddd');

 

insert into test222 values(222,'bbb');

insert into test222 values(333,'ccc');

insert into test222 values(444,'eee');

insert into test222 values(444,'fff');

笛卡尔积

select * from test111,test222;

       111 aaa                         222 bbb

       111 aaa                         333 ccc

       111 aaa                         444 eee

       111 aaa                         444 fff

       222 bbb                         222 bbb

       222 bbb                         333 ccc

       222 bbb                         444 eee

       222 bbb                         444 fff

       444 ddd                         222 bbb

       444 ddd                         333 ccc

       444 ddd                         444 eee

       444 ddd                         444 fff

全连接

select * from test111 full join test222 on test111.id=test222.id;

       222 bbb                         222 bbb

       444 ddd                         444 eee

       444 ddd                         444 fff

       111 aaa                                  

                                       333 ccc  

等同于

select * from test111 inner join test222 on test111.id=test222.id

union all

select * from test111 left join test222 on test111.id=test222.id where test222.id is null

union all

select * from test111 right join test222 on test111.id=test222.id where test111.id is null;


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值