sql語句中any和all的用法

2021-10-03 11:55:42 字數 1107 閱讀 8986

這兩個都是用於子查詢的

any 是任意乙個

all 是所有

any表示有任何乙個滿足就返回true,all表示全部都滿足才返回true  

建議使用max和min更加直觀

比如select * from student where 班級=』01』 and age > all (select age from student where 班級=』02』);

就是說,查詢出01班中,年齡大於 02班所有人的同學

相當於select * from student where 班級=』01』 and age > (select max(age) from student where 班級=』02』);

而select * from student where 班級=』01』 and age > any (select age from student where 班級=』02』);

就是說,查詢出01班中,年齡大於 02班任意乙個 的 同學

相當於select * from student where 班級=』01』 and age > (select min(age) from student where 班級=』02』); 

select cno ,sno ,degree from score sc where sc.cno = '3-105' and  sc.degree>

( select min(degree) from score where cno = '3-245') order by degree desc

select * from score where degree>any(select degree from score where cno='3-245')

and cno='3-105'  order by degree desc;

上面感覺min 更直觀些

select degree from score where degree >  any(select  degree   from score  where cno='3-245') 

select any(select  degree   from score  where cno='3-245')   from score  :  這種寫法就不對了

sql語句中any和all的用法

今天在修改以前同事 時發現用的巢狀查詢,其實本人是不太喜歡用巢狀的。當是跟 發現執行報錯,大本意思是返回的是多條資訊。就在網上查了一下,說用any就可以了,所以晚上學習一下。感謝網上的資料!這兩個都是用於子查詢的 any 是任意乙個 all 是所有 any表示有任何乙個滿足就返回true,all表示...

sql酷斃的any和all

今天下午從6點多想到現在,倆小時啊!我這是什麼腦袋啊!尤其any,真是有點暈,現在懂了。any和all通常與關係運算子一起使用,實現對子查詢返回值的判斷工作,如下 any比子查詢返回的任意結果大就行,即大於返回結果的最小值。any就是等於返回結果的任意值就行,等價於in。all比子查詢返回的所有結果...

python中的any 和all 函式

對於all x 引數x中的所有元素不為0 或false,則返回true,否則返回false。另外,如果x為空物件,也是返回true。例如 a all a b c d print 列表list,元素都不為空或0 a b all a b d print 列表list,存在乙個為空的元素 b c all ...