some,any,all的简单学习

本文介绍了如何使用some, any和all关键字来处理SQL子查询中返回的多行结果,包括具体的用法示例及其等价转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

一:用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);




     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1303335,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值