您的位置:首页 > 其它

some,any,all的简单学习

2013-09-29 11:35 141 查看
/*
一:用some,any 和all对子查询中返回的多行结果进行处理。
1.some在此满足其中一个的意义,是用or串起来的比较从句。
2.any也表示满足其中一个的意义,也是用or串起来的比较从句。
3.all则满足其中所有的查询结果的含义,使用and串起来的比较从句。

例子1:
select * from tableA where fld > all(select fld from tableA);
相当于
select * from tableA where fld > (select max(fld) from tableA);

例子2:
select * from tableA where fld < any(select fld from tableA);
相当于
select * from tableA where fld < (select min(fld) from tableA);

例子3:
select * from tableA where fld = any(select fld from tableA);
相当于
select * from tableA where fld in(select fld from tableA);

*/

/*4.
找出员工中,只要比部门号为10的员工中的任何一个员工的工资高的员工的姓名和工资。
也就是说只要比部门号为10的员工中那个工资最少的员工的工资高的就满足条件。
*/
select ename,sal from emp where sal > any(select sal from emp where deptno = 10);
--其实相当于下面的代码
select ename,sal from emp where sal > (select min(sal) from emp where deptno = 10);
--当然你也可以用some,但是更推荐用any。下面一个方法才是some的常用方法。

/*5.找到和30部门员工的任何一个人的工资相同的那些员工*/
select ename,sal from emp where sal = some(select sal from emp where deptno = 30) and deptno not in(select deptno from emp where deptno = 30);

/*6.找到比部门号20的员工的所有员工的工资都要高的员工*/
select ename,sal from emp where sal > all(select sal from emp where deptno = 20);

本文出自 “我的JAVA世界” 博客,请务必保留此出处http://hanchaohan.blog.51cto.com/2996417/1303335
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: