ORACLE连接 (+)和Join总

一.笛卡尔积

a.oracle写法

scott@ORCL> select count(1) from emp;

  COUNT(1)
----------
        14

scott@ORCL> select count(1) from dept;

  COUNT(1)
----------
         4


scott@ORCL> select count(1) from emp,dept;

  COUNT(1)
----------
        56

b.传统sql写法

scott@ORCL> select count(1) from emp cross join dept;

  COUNT(1)
----------
        56

二.等值连接(也叫作内连接)

a.oracle写法

scott@ORCL> select count(1) from emp a,dept b where a.deptno=b.deptno;

  COUNT(1)
----------
        14

b.传统sql写法

scott@ORCL> select * from emp a join dept b on a.deptno=b.deptno;

else1:等值连接的记录筛选

a.

scott@ORCL> select * from emp a,dept b where a.deptno=b.deptno and a.deptno=30;

b.

scott@ORCL> select * from emp a join dept b on a.deptno=b.deptno where a.deptno=30;

else2:不等值连接

a.

scott@ORCL> select * from emp a,salgrade b where a.sal>=b.losal and a.sal<=b.hisal;

b.

scott@ORCL> select * from emp a join salgrade b on a.sal between b.losal and b.hisal;

三.外连接

1.左外连接(left outer join)

a.

scott@ORCL> select * from emp a,dept b where a.deptno=b.deptno(+);

b.

scott@ORCL> select * from emp a left outer join dept b on a.deptno=b.deptno;

2.右外连接

a.

scott@ORCL> select * from emp a,dept b where a.deptno(+)=b.deptno;

b.

scott@ORCL> select * from emp a right outer join dept b on a.deptno=b.deptno;

3.全外连接

a.

scott@ORCL> select * from emp a,dept b where a.deptno(+)=b.deptno union 
  2  select * from emp a,dept b where a.deptno=b.deptno(+);

b.

scott@ORCL> select * from emp a full outer join dept b on a.deptno=b.deptno;

四.自然连接:根据两个表的相同字段

scott@ORCL> select * from emp natural join dept b;

如果两张表没有相同字段会报错:没有选定行

五.using字句:指定表的某个字段作为连接的字段

scott@ORCL> select * from emp a join dept b using(deptno);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值