sql之join用法完全版【转载】

 SQL中大概有这么几种JOIN

cross join

inner join

left outer join  

right outer join 

full outer join

 

首先都是基于cross join(笛卡尔乘积),然后是inner join,在笛卡尔乘积的结果集中去掉不符合连接条件的行。

left outer join 是在inner join的结果集上加上左表中没被选上的行,行的右表部分每个字段都用NUll填充。

right outer join 是在inner join的结果集上加上右表中没被选上的行,行的左表部分全用NULL填充。


SQL JOIN 用法完全版

一、各种JOIN的含义

SQL中大概有这么几种JOIN

cross join

inner join

left outer join  

right outer join 

full outer join

 

首先都是基于cross join(笛卡尔乘积),然后是inner join,在笛卡尔乘积的结果集中去掉不符合连接条件的行。

left outer join 是在inner join的结果集上加上左表中没被选上的行,行的右表部分每个字段都用NUll填充。

right outer join 是在inner join的结果集上加上右表中没被选上的行,行的左表部分全用NULL填充。

outer的意思就是"没有关联上的行"。

 

二、旧式写法和标准写法:

 

1INNER Join code as the following:

 

Select * from A a, B b where a.categoryID = b.categoryID;

Equals:

Select * from A a inner join B b on a.categoryID = b.categoryID;

 

2OUTER Join code as the following

 

select * from A a full(left/right) outer  join B b  on a on a.categoryID = b.categoryID;

Equals::

Select * from A a, B b where a.categoryID *=  b.categoryID;

Select * from A a, B b where a.categoryID =*  b.categoryID;

 

三、例子

Table A have 12( 8+4) entries, 8 entries have valid relation with B

Table B have 80(77+3) entries , 77 entries have valid relation with A.

 

then the return amount of join is :

cross join : 12*80

inner join : 77

full outer join : 77+4+3

left outer join: 77 + 4

right outrer join: 77 + 3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值