oracle中all、any函数用法与区别说明(oracle all_tables)全程干货

随心笔谈2年前发布 admin
208 0 0

文章摘要

文章介绍了Oracle中`any()`和`all()`两个关键字的用法及其区别,并通过示例解释了它们的应用方式。`any()`表示括号内任何一个条件满足即可,而`all()`表示所有条件都满足。文章通过具体场景说明了`any()`和`all()`在SQL中的应用: 1. `any()`用法 - 大于最小值:`select * from A where id >= any(select id from A)`,等价于`select * from A where id >= (select min(id) from A)`。 - 小于最大值:`select * from A where id <= any(select id from A)`,等价于`select * from A where id <= (select max(id) from A)`。 2. `all()`用法 - 大于最大值:`select * from A where id > all(select id from A)`,等价于`select * from A where id > (select max(id) from A)`。 - 小于最小值:`select * from A where id < all(select id from A)`,等价于`select * from A where id < (select min(id) from A)`。 文章指出`any()`和`all()`的用法较为绕口,难以理解,但通过具体的例子可以更清晰地掌握它们的含义。例如: - `select * from t_hq_ryxx where gongz > any(select pingjgz from t_hq_bm)` 的结果是所有大于字段`pingjgz`中最小值的值。 - `select * from t_hq_ryxx where gongz < all(select pingjgz from t_hq_bm)` 的结果是所有小于字段`pingjgz`中最大值的值。 文章还提到,通过将`any()`和`all()`的用法与最大值、最小值的比较相结合,可以更直观地理解它们的功能。



在Oracle中,any()表示括号内任何一个条件,只要有一个满足即可;而all()表示所有的条件都满足才可以。

1.all用法

–大于最大值
select * from A where id >=all(select id from A)
–这相当于
select * from A where id >=(select max(id) from A)

–小于最小值
select * from A where id <=all(select id from A)
–这相当于
select * from A where id <=(select min(id) from A)

2.any用法 

–大于任意一个数即可,大于最小值
select * from A where id >=any(select id from A)
–这相当于
select * from A where id >=(select min(id) from A)

–小于任意一个数即可,小于最大值
select * from A where id <=any(select id from A)
–这相当于
select * from A where id <=(select max(id) from A)

对于any,all的用法,书中说的比较绕口,难以理解,如果通过举例就会比较清晰.

any的例子:

select * from t_hq_ryxx where gongz > any (select pingjgz from t_hq_bm);

输出的结果是所有大于字段‘pingjgz‘中最小数的值,简单来说就是输出的数全部大于‘pingjgz‘字段中的最小值;

select * from t_hq_ryxx where gongz < all (select pingjgz from t_hq_bm);

输出的结果是所有小于字段‘pingjgz‘中最大数的值,简单来说就是输出的数全部小于‘pingjgz‘字段中的最大值;

即:大于最小值,小于最大值

all的例子:

select * from t_hq_ryxx where gongz > all (select pingjgz from t_hq_bm);

输出的结果是所有大于字段‘pingjgz‘中最大数的值,简单来说就是输出的数全部大于‘pingjgz‘字段中的最大值;

select * from t_hq_ryxx where gongz < all (select pingjgz from t_hq_bm);

输出的结果是所有小于字段‘pingjgz‘中最小数的值,简单来说就是输出的数全部小于‘pingjgz‘字段中的最小值;

即:小于最小值,大于最大值

如果还是不清楚,把下面这句看懂也就够了:

any 就是匹配集合中的任意一个就满足条件了;而 all 要跟所有的都比较,所有都满足以后才为真。

© 版权声明

相关文章