淺析被遺忘的SQLServer比較運算符修飾詞
更新時間:2013年06月26日 17:50:52 作者:
本篇文章是對被遺忘的SQLServer比較運算符修飾詞進行了詳細的分析介紹,需要的朋友參考下
SQLServer中有三個關(guān)鍵字可以修改比較運算符:All、Any和Some,其中Some和Any等價。
官方的參考文檔
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運算符和子查詢之間,作用類似Exists、not exists、in、not in以及其他邏輯意義,這些語法同樣被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 ) --無數(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)的比較運算將不會返回任何結(jié)果,原因就不用多解釋了。而因為t1和t2表的null的存在他們和not exists之類的比較符會有一些區(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)
他們邏輯上意義很像但是對于null的處理卻是恰恰相反,第一句會忽略子查詢的null而把t2的null同時查出來,第二句卻是忽略了t2的null同時會因為t1中的null而無法查詢到數(shù)據(jù)。
官方的參考文檔
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運算符和子查詢之間,作用類似Exists、not exists、in、not in以及其他邏輯意義,這些語法同樣被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 ) --無數(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)的比較運算將不會返回任何結(jié)果,原因就不用多解釋了。而因為t1和t2表的null的存在他們和not exists之類的比較符會有一些區(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)
他們邏輯上意義很像但是對于null的處理卻是恰恰相反,第一句會忽略子查詢的null而把t2的null同時查出來,第二句卻是忽略了t2的null同時會因為t1中的null而無法查詢到數(shù)據(jù)。
相關(guān)文章
SQL Server誤區(qū)30日談 第28天 有關(guān)大容量事務(wù)日志恢復模式的誤區(qū)
在大容量事務(wù)日志恢復模式下只有一小部分批量操作可以被“最小記錄日志”,這類操作的列表可以在Operations That Can Be Minimally Logged找到。這是適合SQL Server 2008的列表,對于不同的SQL Server版本,請確保查看正確的列表2013-01-01SQLServer行列互轉(zhuǎn)實現(xiàn)思路(聚合函數(shù))
這篇文章主要為大家詳細介紹了SQLServer行列互轉(zhuǎn)實現(xiàn)思路,使用聚合函數(shù)pivot/unpivot實現(xiàn)行列互轉(zhuǎn),感興趣的小伙伴們可以參考一下2016-03-03SQL Server誤區(qū)30日談 第18天 有關(guān)FileStream的存儲,垃圾回收以及其它
由于FileStream數(shù)據(jù)容器(指的是存放FileStream文件的NTFS文件夾,杜撰出來的術(shù)語)必須像數(shù)據(jù)文件或日志文件那樣符合本地存儲策略-也就是說,這個數(shù)據(jù)容器必須放在對于運行SQL Server的Windows Server是本地存儲2013-01-01sqlserver 無法驗證產(chǎn)品密匙的完美解決方案[測試通過]
Win2003 SQL2000時CD-KEY(序列號)無法驗證的問題的解決方法2009-07-07SQLServer中JSON文檔型數(shù)據(jù)的查詢問題解決
SQL Server 對于數(shù)據(jù)平臺的開發(fā)者來說越來越友好,下面這篇文章主要給大家介紹了關(guān)于SQLServer中JSON文檔型數(shù)據(jù)的查詢問題的解決方法,需要的朋友可以參考下2021-06-06SQLSERVER 創(chuàng)建索引實現(xiàn)代碼
是SQL Server編排數(shù)據(jù)的內(nèi)部方法。它為SQL Server提供一種方法來編排查詢數(shù)據(jù)2012-04-04