sql中幾個(gè)子查詢關(guān)鍵詞any,all,exists,in
in
in表示值是否存在子查詢結(jié)果集中
select * from A where name in(select name from B)
exists
exists是表示子查詢是否返回結(jié)果,而不管返回的具體內(nèi)容。
select *from A where exists( select 1 from B where name='liu');
in是“=any的簡(jiǎn)寫(xiě)”;
not in是“<>all”的簡(jiǎn)寫(xiě)
any是表示子查詢結(jié)果中任意一個(gè),all表示子查詢結(jié)果中的所有。
>any表示只要大于子查詢結(jié)果中的任一個(gè),表達(dá)式就成立,=any表示等于子查詢中的任一個(gè),相當(dāng)于in.
all與any
>all表示要大于子查詢結(jié)果中的所有,才會(huì)返回true,? not in 相當(dāng)于“<>all”.
當(dāng)ALL、ANY或SOME操作符與"="比較操作符配合使用時(shí),子查詢可以是一個(gè)數(shù)據(jù)表子查詢。此時(shí),你需要使用一個(gè)數(shù)據(jù)行構(gòu)造器來(lái)提供與子查詢所返回的數(shù)據(jù)行進(jìn)行比較的比較值。
eg:select *from A where (name,num)=any(select name1,num1 from B where name='john');
in在子查詢不返回?cái)?shù)據(jù)的時(shí)候,為false,子查詢結(jié)果中有null的時(shí)候,null不會(huì)用于比較。
any 同樣在子查詢不返回?cái)?shù)據(jù)的時(shí)候,為false,子查詢結(jié)果中有null的時(shí)候,null不會(huì)用于比較。
all在子查詢不返回?cái)?shù)據(jù)的時(shí)候,為true,子查詢結(jié)果中有null的時(shí)候,不會(huì)返回?cái)?shù)據(jù)。
not in 或not exists來(lái)代替.
not in 不等于<> any,相當(dāng)于<>all,
<>any是只要不等于其中的任意一個(gè),就成立