被遺忘的SQLServer比較運(yùn)算符謂詞
更新時(shí)間:2009年08月27日 00:44:15 作者:
SQLServer中有三個(gè)關(guān)鍵字可以修改比較運(yùn)算符:All、Any和Some,其中Some和Any等價(jià)。
官方的參考文檔
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運(yùn)算符和子查詢之間,作用類似Exists、not exists、in、not in以及其他邏輯意義,這些語(yǔ)法同樣被SQLServer2000支持但是很少看到有人用它們。
set nocount on
use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
create table t1 (n int )
insert into t1 select 2 union select 3
if (object_id ('t2' ) is not null ) drop table t2
create table t2 (n int )
insert into t2 select 1 union select 2 union select 3 union select 4
select * from t2 where n> all (select n from t1 ) --4
select * from t2 where n> any (select n from t1 ) --3,4
--select * from t2 where n>some(select n from t1) --3,4
select * from t2 where n= all (select n from t1 ) --無(wú)數(shù)據(jù)
select * from t2 where n= any (select n from t1 ) --2,3
--select * from t2 where n=some(select n from t1) --2,3
select * from t2 where n< all (select n from t1 ) --1
select * from t2 where n< any (select n from t1 ) --1,2
--select * from t2 where n<some(select n from t1) --1,2
select * from t2 where n<> all (select n from t1 ) --1,4
select * from t2 where n<> any (select n from t1 ) --1,2,3,4
--select * from t2 where n<>some(select n from t1)--1,2,3,4
set nocount off
注意,如果t1中包含null數(shù)據(jù),那么所有All相關(guān)的比較運(yùn)算將不會(huì)返回任何結(jié)果,原因就不用多解釋了。而因?yàn)閠1和t2表的null的存在他們和not exists之類的比較符會(huì)有一些區(qū)別。
比如下面兩句
select * from t2 a where not exists(select 1 from t1 where n>=a.n)
select * from t2 where n >all(select n from t1)
他們邏輯上意義很像但是對(duì)于null的處理卻是恰恰相反,第一句會(huì)忽略子查詢的null而把t2的null同時(shí)查出來(lái),第二句卻是忽略了t2的null同時(shí)會(huì)因?yàn)閠1中的null而無(wú)法查詢到數(shù)據(jù)。
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運(yùn)算符和子查詢之間,作用類似Exists、not exists、in、not in以及其他邏輯意義,這些語(yǔ)法同樣被SQLServer2000支持但是很少看到有人用它們。
復(fù)制代碼 代碼如下:
set nocount on
use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
create table t1 (n int )
insert into t1 select 2 union select 3
if (object_id ('t2' ) is not null ) drop table t2
create table t2 (n int )
insert into t2 select 1 union select 2 union select 3 union select 4
select * from t2 where n> all (select n from t1 ) --4
select * from t2 where n> any (select n from t1 ) --3,4
--select * from t2 where n>some(select n from t1) --3,4
select * from t2 where n= all (select n from t1 ) --無(wú)數(shù)據(jù)
select * from t2 where n= any (select n from t1 ) --2,3
--select * from t2 where n=some(select n from t1) --2,3
select * from t2 where n< all (select n from t1 ) --1
select * from t2 where n< any (select n from t1 ) --1,2
--select * from t2 where n<some(select n from t1) --1,2
select * from t2 where n<> all (select n from t1 ) --1,4
select * from t2 where n<> any (select n from t1 ) --1,2,3,4
--select * from t2 where n<>some(select n from t1)--1,2,3,4
set nocount off
注意,如果t1中包含null數(shù)據(jù),那么所有All相關(guān)的比較運(yùn)算將不會(huì)返回任何結(jié)果,原因就不用多解釋了。而因?yàn)閠1和t2表的null的存在他們和not exists之類的比較符會(huì)有一些區(qū)別。
比如下面兩句
select * from t2 a where not exists(select 1 from t1 where n>=a.n)
select * from t2 where n >all(select n from t1)
他們邏輯上意義很像但是對(duì)于null的處理卻是恰恰相反,第一句會(huì)忽略子查詢的null而把t2的null同時(shí)查出來(lái),第二句卻是忽略了t2的null同時(shí)會(huì)因?yàn)閠1中的null而無(wú)法查詢到數(shù)據(jù)。
相關(guān)文章
SQL?Server數(shù)據(jù)庫(kù)死鎖的原因及處理方法
SQL Server數(shù)據(jù)庫(kù)死鎖是指兩個(gè)或多個(gè)事務(wù)相互等待對(duì)方持有的資源,從而導(dǎo)致它們都無(wú)法繼續(xù)執(zhí)行的情況,下面這篇文章主要給大家介紹了關(guān)于SQL?Server數(shù)據(jù)庫(kù)死鎖的原因及處理方法,需要的朋友可以參考下2024-08-08
SQL Server日期時(shí)間格式轉(zhuǎn)化的方式小結(jié)
在SQL Server中,日期格式轉(zhuǎn)換可以使用CONVERT函數(shù)和CAST函數(shù),本文呢給大家介紹了SQL Server日期時(shí)間格式轉(zhuǎn)化的方式,并通過(guò)代碼示例講解非常詳細(xì),需要的朋友可以參考下2024-03-03
sqlserver數(shù)據(jù)庫(kù)移動(dòng)數(shù)據(jù)庫(kù)路徑的腳本示例
前段時(shí)間做過(guò)這么一件事情,把原本放在c盤的所有數(shù)據(jù)庫(kù)(除了sql server系統(tǒng)文件外)文件Move到D盤,主要是為了方便后續(xù)管理以及減少磁盤I/O阻塞(C,D是2個(gè)獨(dú)立磁盤)。腳本需輸入2個(gè)參數(shù):目標(biāo)數(shù)據(jù)庫(kù)名字和目標(biāo)目錄2013-12-12
MSSQLSERVER跨服務(wù)器連接(遠(yuǎn)程登錄)的示例代碼
這篇文章主要介紹了MSSQLSERVER跨服務(wù)器鏈接服務(wù)器的方法,大家參考使用2013-11-11
SQL SERVER實(shí)現(xiàn)連接與合并查詢
本文詳細(xì)講解了SQL SERVER實(shí)現(xiàn)連接與合并查詢的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
SQL?Server開(kāi)發(fā)智能提示插件SQL?Prompt介紹
這篇文章介紹了SQL?Server開(kāi)發(fā)智能提示插件SQL?Prompt,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
SQL創(chuàng)建的幾種存儲(chǔ)過(guò)程
表名和比較字段可以做參數(shù)的存儲(chǔ)過(guò)程2010-05-05
sql server中判斷表或臨時(shí)表是否存在的方法
這篇文章主要介紹了sql server中判斷表或臨時(shí)表是否存在的方法,需要的朋友可以參考下2015-11-11

