sql的复杂查询使用案列与技巧

 

本片博客使用的数据库表是oracle中的scott用户表;

 

  

    -------------------  自然连接查询

          查询 smith 的上司(两种方法)

  

----查询SMITH的上司
    select ename from emp where empno=(select mgr from emp where ename  = 'SMITH');
    
    --自连接查询
      select e1.ename, e1.mgr,e2.ename from emp e1 ,emp e2 where e1.mgr= e2.empno and E1.ename = 'SMITH';

 

 

 

--------------------------in的使用

  ---查询与部门10工作相同的员工的信息

       分组查询

select ename,job,sal,deptno from emp where job in(select job from emp where deptno = 10);

  

 

-----------------------分组函数和all的查询

--查找比部门30工资高的所有员工的信息

 

 

select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno = 30);
all查询;
select ename,sal,deptno from emp where sal> all(select sal from emp where deptno = 30);

 

 --------------------函数和any

--查找比部门30任意的工资高的所有员工的信息   

          

  ----使用any或者
        select ename,sal,deptno from emp where sal> any ( select sal from emp where deptno = 30);
       
---使用函数
       select ename,sal,deptno from emp where sal>( select min(sal) from emp where deptno = 30);
       

 

 

-----------------------------------------------------------------伪列查询  rownum  rowid
---查询部分数据
  -- 查询第五到第十条的数据
  

 select ROWNUM,ename,job from(select ROWNUM r ,ename,job from emp where ROWNUM<=10) where r>5;
    
  
  -----MYsql + sqlite分页查询的方法
    
     -- select * from emp limit 1,10;

 

 

-----------------------------------------------------------合并查询操作符 union ,unuion all,intersect,minus

union ,union all合并表的查询


select * from emp where job ='SALESMAN';
select * from emp where sal>1500;


union的合并查询  合并并且去除重复的行
  
select * from emp where job ='SALESMAN' union select * from emp where sal>1500;


union all的合并查询  取所有的数据
  
select * from emp where job ='SALESMAN' union all select * from emp where sal>1500;


interseect取交集查询
select * from emp where job ='SALESMAN' intersect select * from emp where sal>1500;


minus查询出来的结果是只显示在第一个结果集中存在 而不在第二个结果集重负存在的

select * from emp where job ='SALESMAN' minus select * from emp where sal>1500;

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值