oracle表连接

多表连接:笛卡尔集、表连接
表连接:
select 字段列表
from table1,table2
where table.column1=tables.column2;

oracle9i连接形式(sql99规范):
交叉连接(Cross join)
自然连接(Natural join)
使用Using子句建立连接
使用On子句建立连接
外连接(outer join):左外连接、右外连接、全外连接
oracle8i之前的表连接:
等值连接(Equijoin)
非等值连接(Non-Equijoin)
外连接(Outer join):左外连接、右外连接
自连接(Selfjoin)

等值连接:
select empno,ename,sal,emp.deptno,dname from emp,dept where emp.deptno=dept.deptno;
非等值连接:
select emp.empno,emp.ename,emp.sal,salgrade.grade,salgrade.losal,salgrade.hisal from

emp,salgrade where emp.sal between salgrade.losal and salgrade.hisal;
外连接,连接运算符(+)
自连接:
select a.empno,a.name,a.mgr,b.ename from emp a,empb where a.mgr=b.empno;

sql99:
cross join结果产生笛卡尔集:
select empno,ename,sal,emp.emptno,dname from emp cross join dept;
natrual join,基于两个表中全部同名列建立连接:
从两个表中选出同名列的值均对应相等的所有行
如果两个表中同名列的数据类型不同,则出错
不允许在参照列上使用表名或者别名作为前缀
select empno,ename,sal,deptno,dname from emp natrual join dept;
select empno,ename,deptno,dname from emp join dept using(deptno);  --使用同名列deptno进

行等值连接
select empno,ename,sal,emp.deptno,dname from emp join dept on(emp.deptno=dept.deptno); 

--可以使用任意连接条件,可以是相同列或不同列,可以是等值连接也可以是不等值连接
多表连接:
select 字段列表
from table1
[cross join table2]|
[natural join table2]|
[join table2 using (字段名)]|
[join table2 on(table1.column_name=table2.column_name]|
[(left|right|full outer)join table2 on (table1.column_name=table2.column_name)]
[cross join table3]|
[natural join table3]|
[join table3 using(字段名)]|
[join table3 on(table2.column_name=table3.column_name)]|
[(left|right|full outer)join table3 on (table2.column_name=table3.column_name)];

inner join与outer join:
inner join只返回满足条件的数据
select employee_id,last_name,salary,department_id,department_name from employees join

departments using(department_id);
outer join:
左外连接:返回满足条件的行和左表中不满足条件的行
select employee_id,last_name,salary,department_id,department_name from employees left

join departments using(department_id);
右外连接:返回满足条件的行和右表中不满足条件的行
select employee_id,last_name,salary,department_id,department_name from employees right

join departments using(department_id);
满外连接(Full Outer Join):返回所有满足条件的行与所有不满足条件的行
select employee_id,last_name,salary,department_id,department_name from employees full

outer join departments using(department_id);

子查询(Sub Query)
子查询在主查询前执行一次
主查询使用子查询的结果
select 字段列表 from table where 表达式 operator(select 字段列表 from table where ...);
select * from emp where sal >(select sal from emp where empno=7654);
子查询使用注意事项:
在查询是基于未知值时考虑使用子查询
子查询必须包括在括号内
子查询放在比较运算符的右侧
除非进行Top-N分析,否则不要在子查询中使用order by子句
对单行子查询使用单行运算符
对多行子查询使用多行运算符
单行子查询:只返回一行记录
单行记录比较运算符:=  >  >=  <  <=  <>
select * from emp where sal>(select sal from emp where empno=7566);
子查询空值/多值问题:
如果子查询未返回任何行,则主查询也不返回任何结果
select * from emp where sal>(select sal from emp where empno=8888);
如果子查询返回单行结果,则为单行子查询,可以在主查询中对其使用相应的单行记录比较运算符
select * from emp where sal>(select sal from emp where empno=7566);
如果子查询返回多行记录,则为多行子查询,此时不允许对其使用单行记录比较运算符
select * from emp where sal>(select avg(sal) from emp group by deptno);  --非法
多行子查询:
多行记录比较运算符 in(等于列表中的任何一个)  any(和子查询返回的任意一个值比较)  all(和子

查询返回的所有值比较)
select * from emp where sal>any(select avg(sal) from emp group by deptno);
select * from emp where sal>all(select avg(sal) from emp group by deptno);
select * from emp where job in (select job from emp where ename='martin' or

ename='smith');
TopN查询:
select 字段列表 from (select 字段列表 from table order by 排序字段) where rownum<=n;
select * from (select * from emp order by sal desc) where rownum<=5);
select子查询结果相当于一个临时表,rownum是一个伪列,返回符合结果的行号,rownum只能选取小于

等于不能是其他,因为rownum始终从1开始计数
取查询中间某一段记录方法,将伪列变为真实列:
select * from (select rownum as myno,a.* from (select * from emp order by sal desc) a)

where myno>5 and myno<=10;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值