被遺忘的SQLServer比較運算符謂詞
更新時間:2009年08月27日 00:44:15 作者:
SQLServer中有三個關鍵字可以修改比較運算符: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相關的比較運算將不會返回任何結果,原因就不用多解釋了。而因為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相關的比較運算將不會返回任何結果,原因就不用多解釋了。而因為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ù)。
相關文章
SQL Server定時收縮數(shù)據(jù)庫日志為指定大小的示例代碼
SQL Server提供了DBCC SHRINKFILE 命令來清理事務日志文件,該命令可以縮小指定文件的大小,并釋放磁盤空間,本文給大家介紹了SQL Server如何定時收縮數(shù)據(jù)庫日志為指定大小,需要的朋友可以參考下2024-03-03Sql Server 2016新功能之Row-Level Security(值得關注)
Sql Server 2016 有一個新功能叫 Row-Level Security 。下面通過實例給大家介紹Sql Server 2016新功能之Row-Level Security,感興趣的朋友一起看看吧2016-11-11MSSQL數(shù)據(jù)庫占用內存過大造成服務器死機問題的解決方法
有時候我們的服務器使用MSSQL數(shù)據(jù)庫,但如果MSSQL數(shù)據(jù)庫占用內存過大可能導致服務器死機,這里分享下解決方法, 需要的朋友可以參考下2013-07-07在SQL Server中查詢資料庫的TABLE數(shù)量與名稱的sql語句
這篇文章主要介紹了在SQL Server中查詢資料庫的TABLE數(shù)量與名稱的sql語句,需要的朋友可以參考下2014-04-04SQLServer中匯總功能的使用GROUPING,ROLLUP和CUBE
查看SQL Server的幫助才發(fā)現(xiàn),厲害啊,原來還有這么厲害的東西,不由的想起以前做水晶報表的時候,原來在SQL Server中就可以實現(xiàn)這樣的功能.2010-07-07