加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle数据库关联查询

发布时间:2020-12-12 13:37:39 所属栏目:百科 来源:网络整理
导读:查询语句的组成 ? select * from tname [where 条件] [group by colName] [having条件] [ordey by colName] 子句的执行顺序: 1:from子句,内部是从右向左,从后往前执行 2:where子句,内部是从右向左,从后往前执行,如果多个条件时将过滤记录数比较多的条
查询语句的组成

?

  

  select * from tname [where 条件] [group by colName] [having条件] [ordey by colName]

  子句的执行顺序:

      1:from子句,内部是从右向左,从后往前执行

      2:where子句,内部是从右向左,从后往前执行,如果多个条件时将过滤记录数比较多的条件放在最右边。

      3:group by子句,如果有多个字段,从左往右

      4:having子句,在效率上比较低,我们应该尽量避免

      5:select子句:通配符*表示所有字段,所以也应该尽可能的避免

      6:oreder by子句:只可以使用select子句中的字段别名

?

关联查询基础

?

?

  表emp,dept。

  1:等值连接

    select * from emp,dept where emp.deptno=dept.deptno;

  2:内连接

    select * from emp inner join dept on (emp.deptno=dept.deptno);

  3:左外连接

    select * from emp left outer join dept on (emp.deptno=dept.deptno);

    select * from emp,dept where emp.deptno=dept.deptno(+);--加括号的为从表

  4:全外连接

    select * from emp e full outer join dept d on (e.deptno=d.deptno);

  5:自连接

    select * from emp e join emp d on e.empno=d.mgr;

高级关联查询

?

?

  1:子查询在where子句中

    eg:select * from emp where sal>(select sal from emp where ename = ‘JONES‘);紫色sql语句返回一个值与sal进行比较

  2:子查询在having子句中

    eg:select ?deptno,avg(nvl(sal,0)) from emp group by deptno having avg(nvl(sal,0))>(select avg(nvl(sal,0)) from emp where deptno=10);?紫色sql语句返回一个值与avg(sal)进行比较

  3:子查询在from子句中

    eg:select * from emp,dept,(select ?deptno from emp group by deptno having avg(nvl(sal,0))>2000) f where emp.deptno=dept.deptno and dept.deptno = f.deptno;紫色sql语句返回一个只有一个字段deptno的表

  4:子查询在select子句中

    eg:select e.ename,e.job,(select d.dname from dept d where d.deptno = e.deptno) from emp e;紫色sql语句返回要查询的字段内容

?

exists用法

?

?

  exists表示()内子查询语句为真就会返回true,where条件成立就会执行主sql语句,如果为空就不会执行sql语句,not exists恰恰与exists相反。

  

  eg:select * from emp where exists(select * from emp where id=1001);

    emp表中存在id=1001的数据,返回真就会执行主sql语句,不存在就不会执行。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读