`

any all some

阅读更多
原文地址:http://www.leadnt.com/2008/12/sql%e4%b8%adall-any-some%e7%9a%84%e6%84%8f%e6%80%9d/
原文作者:杜建宇

可以用 ALL 或 ANY 关键字修改引入子查询的比较运算符。SOME 是与 ANY 等效的 ISO 标准。
通过修改的比较运算符引入的子查询返回零个值或多个值的列表,并且可以包括 GROUP BY 或 HAVING 子句。这些子查询可以用 EXISTS 重新表述。
以 > 比较运算符为例,>ALL 表示大于每一个值。换句话说,它表示大于最大值。例如,>ALL (1, 2, 3) 表示大于 3。>ANY 表示至少大于一个值,即大于最小值。因此 >ANY (1, 2, 3) 表示大于 1。
若要使带有 >ALL 的子查询中的行满足外部查询中指定的条件,引入子查询的列中的值必须大于子查询返回的值列表中的每个值。
同样,>ANY 表示要使某一行满足外部查询中指定的条件,引入子查询的列中的值必须至少大于子查询返回的值列表中的一个值。


create table #A(id int)
go
insert into #A values(1)
insert into #A values(2)
insert into #A values(3)
insert into #A values(4)
go
--All:对所有数据都满足条件,整个条件才成立,例如:5大于所有返回的id
select *
from #A
where 5>All(select id from #A)
go
--Any:只要有一条数据满足条件,整个条件成立,例如:3大于1,2
select *
from #A
where 3>any(select id from #A)
go
--Some和Any一样
/*引用Mcrosoft的官方解释:*/
可以用 ALL 或 ANY 关键字修改引入子查询的比较运算符。SOME 是与 ANY 等效的 ISO 标准。
通过修改的比较运算符引入的子查询返回零个值或多个值的列表,并且可以包括 GROUP BY 或 HAVING 子句。这些子查询可以用 EXISTS 重新表述。
以 > 比较运算符为例,>ALL 表示大于每一个值。换句话说,它表示大于最大值。例如,>ALL (1, 2, 3) 表示大于 3。>ANY 表示至少大于一个值,即大于最小值。因此 >ANY (1, 2, 3) 表示大于 1。
若要使带有 >ALL 的子查询中的行满足外部查询中指定的条件,引入子查询的列中的值必须大于子查询返回的值列表中的每个值。
同样,>ANY 表示要使某一行满足外部查询中指定的条件,引入子查询的列中的值必须至少大于子查询返回的值列表中的一个值。
下面的查询提供一个由 ANY 修改的比较运算符引入的子查询的示例。它查找定价高于或等于任何产品子类别的最高定价的产品。
USE AdventureWorks;
GO
SELECT Name
FROM Production.Product
WHERE ListPrice >= ANY
(SELECT MAX (ListPrice)
FROM Production.Product
GROUP BY ProductSubcategoryID)对于每个产品子类别,内部查询查找最高定价。外部查询查看所有这些值,并确定定价高于或等于任何产品子类别的最高定价的单个产品。如果 ANY 更改为 ALL,查询将只返回定价高于或等于内部查询返回的所有定价的那些产品。
如果子查询不返回任何值,那么整个查询将不会返回任何值。
=ANY 运算符与 IN 等效。例如,若要查找 Adventure Works Cycles 生产的所有轮子产品的名称,可以使用 IN 或 =ANY。
--Using =ANY
USE AdventureWorks;
GO
SELECT Name
FROM Production.Product
WHERE ProductSubcategoryID =ANY
(SELECT ProductSubcategoryID
FROM Production.ProductSubcategory
WHERE Name = 'Wheels')
--Using IN
USE AdventureWorks;
GO
SELECT Name
FROM Production.Product
WHERE ProductSubcategoryID IN
(SELECT ProductSubcategoryID
FROM Production.ProductSubcategory
WHERE Name = 'Wheels')下面是任一查询的结果集:
Name
--------------------------------------------------
LL Mountain Front Wheel
ML Mountain Front Wheel
HL Mountain Front Wheel
LL Road Front Wheel
ML Road Front Wheel
HL Road Front Wheel
Touring Front Wheel
LL Mountain Rear Wheel
ML Mountain Rear Wheel
HL Mountain Rear Wheel
LL Road Rear Wheel
ML Road Rear Wheel
HL Road Rear Wheel
Touring Rear Wheel
(14 row(s) affected)但是,< >ANY 运算符则不同于 NOT IN:< >ANY 表示不等于 a,或者不等于 b,或者不等于 c。NOT IN 表示不等于 a、不等于 b 并且不等于 c。<>ALL 与 NOT IN 表示的意思相同。
例如,以下查询查找位于任何销售人员都不负责的地区的客户。
Use AdventureWorks;
GO
SELECT CustomerID
FROM Sales.Customer
WHERE TerritoryID <> ANY
(SELECT TerritoryID
FROM Sales.SalesPerson)结果包含除销售地区为 NULL 的客户以外的所有客户,因为分配给客户的每个地区都由一个销售人员负责。内部查询查找销售人员负责的所有销售地区,然后对于每个地区,外部查询查找不在任一地区的客户。
由于同一原因,当在此查询中使用 NOT IN 时,结果将不包含任何客户。
还可以使用 < >ALL 运算符获得相同的结果,该运算符与 NOT IN 等效。
/*其他资料:*/
带【any】的嵌套查询和【some】的嵌套查询功能是一样的。早期的SQL仅仅允许使用【any】,后来的版本为了和英语的【any】相区分,引入了【some】,同时还保留了【any】关键词。
分享到:
评论

相关推荐

    sql中all,any,some用法

    –All:对所有数据都满足条件,整个条件才成立,例如:5大于所有返回的id select * from #A where 5&gt;All(select id from #A) go –Any:只要有一条数据满足条件,整个条件成立,例如:3大于1,2 select * from #A ...

    All in One SEO Pack 1.6.12.2 附中文汉化包.rar

    For advanced users, you can fine-tune everything You can override any title and set any META description and any META keywords you want. Backward-Compatibility with many other plugins, like Auto Meta...

    Python函数any()和all()的用法及区别介绍

    这时候使用python的内置函数any()会非常的简洁: fruits = ['apple', 'orange', 'peach'] str = I want some apples if any(element in str for element in fruits): print string contains some fruits. any() ...

    Can Big Data Do It All

    is located somewhere in “The Cloud” and is made up of a host of “Algorithmic Calculators” that can crunch any bit of information, from any source, at any time, at practically no cost and spit ...

    MySQL中列子查询与行子查询操作的学习教程

    MySQL 列子查询及 IN、ANY、SOME 和 ALL 操作符的使用 MySQL 列子查询 列子查询是指子查询返回的结果集是 N 行一列,该结果通常来自对表的某个字段查询返回。 一个列子查询的例子如下: SELECT * FROM article ...

    Android代码-all-base-adapter

    Some base Adapters apply to any ViewGroup. Such as LinearLayout, ScrollView, and custom ViewGroups. Of course, including RecyclerView, ListView .. 一些Base Adapter,适用于任意ViewGroup。像LinearLayout...

    数据库课本例题(数据查询)

    3.嵌套查询(带有IN谓词的子查询、带有比较运算符的子查询、带有ANY(SOME)或ALL谓词的子查询、带有EXISTS谓词的子查询) 4、集合查询(并操作UNION、交操作INTERSECT、差操作EXCEPT) 5.基于派生表的查询

    Oracle中的优化器如何进行评估优化

    本文将讨论优化器如何评估优化如下的情况和表达式:常量 LIKE 操作符 IN 操作符 ANY和SOME 操作符 ALL 操作符 BETWEEN 操作符 NOT 操作符;传递(Transitivity) 确定性(DETERMINISTIC)函数。

    Extract all these filed to a directory (CppUnitLite)

    Move StackMain.cpp, StackTest.cpp and Stack.h to some other directory Create a workspace Create a project for a static library called CppUnitLite, turn off any pre-compiled headers stuff Add all the ...

    OSTOpenFileToolInstall Demo version

    Since all data recovery activities are performed offline, there is no need to worry about the privacy of your personal information, it is very important when you work with business critical files or ...

    Developer’s Guide to Microsoft Unity

    This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes. © 2013 ...

    lordpe25th march 2001

    As it seems that some people are using it, I tried to recode all the stuff. It's name is "LordPE". I decided to code this project in C because a big part of it is GUI shit. LordPE is not finished up ...

    sql集合运算符使用方法

    今天介绍数据库里面的集合运算符,它是指匹配集合的每一个结果。数据库集合运算符包括:IN、ANY、SOME、ALL以及EXISTS等

    增删改查 limit限制

    数据库表的增删改查,各种用法,简单介绍any,some,all关键字,以及 not in 操作

    All New Electronics Self Teaching Guide (Self-Teaching Guides) by Harry Kybett

    The Q & A format is one of those things that always seems to be a bit unique in any particular book, and this one is no exception. In the early chapters many of the questions may seem insulting in ...

    String matching - Aho & Corasick

    ** 6) I don't subscribe to any one method is best for all searching needs, ** the data decides which method is best, ** and we don't know until after the search method has been tested on the specific ...

    蚁后算法 智能计算 算法 优化方法

    Some female ants do not need to mate to produce offspring, reproducing through asexual parthenogenesis or cloning and all of those offspring will be female. Ant offspring develop from larvae ...

    The Defineitive Guild to SOA

    Enterprise service buses (ESBs) are all the rage in modern software ...all have with any emerging technology), and each technology ended up becoming a reliable tool to solve a specific set of problems.

    vm安装macos补丁_unlock-all-v111

    In all cases make sure VMware is not running, and any background guests have been shutdown. 2. Windows ---------- On Windows you will need to either run cmd.exe as Administrator or using Explorer ...

Global site tag (gtag.js) - Google Analytics