SQL中IN和ANY运算符的区别
SQL 中的 IN
和 ANY
运算符有什么区别?
What is the difference between IN
and ANY
operators in SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
SQL 中的 IN
和 ANY
运算符有什么区别?
What is the difference between IN
and ANY
operators in SQL?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
SQL> -- 您必须在 ANY 之前放置 =、<>、<、>、<= 或 >= 运算符。
对于 In 运算符
但对于 IN 运算符,您不能使用 = 、<>、<、>、<= 或 >=
SQL> -- You must place an =, <>, <, >, <=, or >= operator before ANY.
For In Operator
But with the IN operator you cannot use =, <>, <, >, <=, or >=
IN - 等于列表中的任何内容
ANY - 将值与每个值进行比较由子查询返回。
ALL - 将值与子查询返回的每个值进行比较。
例如:
IN:
显示与部门投资最少的薪资相匹配的所有员工的详细信息?
任何:
获取收入低于最高收入经理的所有员工的详细信息?
获取第 10 部门所有收入高于最低工资的员工的详细信息?
注意:也可以使用
SOME
代替任何
。IN - Equal to anything in the list
ANY - Compares value to each value returned by the sub query.
ALL - Compares value to every value returned by the sub query.
For example:
IN:
Display the details of all employees whose salaries are matching with the least investments of departments?
ANY:
Get the details of all employees who are earning less than the highest earning manager?
Get the details of all employees who are earning more than the least paid in Department 10?
Note:
SOME
can also be used instead ofANY
.也许为了更好地理解,这两个条件是等效的。使用哪一个是一个品味问题(前提是 RDBMS 支持两者)
,
实际上我个人的习惯是使用
IN
来表示列表表达式(例如WHERE x IN (2, 4,6,8)
和=ANY
,分别用于子查询。Maybe for better understanding, these two conditions are equivalent. It's a matter of taste which one you use (provided the RDBMS supports both of them)
and these also
Actually my personal habit is to use
IN
for list expression (likeWHERE x IN (2,4,6,8)
and=ANY
, resp.<>ALL
for sub-queries.使用 all 时
SELECT empno, sal
来自员工
哪里萨尔>全部(2000、3000、4000);
它将返回相当于查询的结果:
SELECT empno, sal
来自员工
哪里萨尔> 2000年和萨尔> 3000 AND 盐> 4000;
使用任意时
SELECT empno, sal
来自员工
哪里萨尔>任意(2000、3000、4000);
相同的结果
返回与SELECT empno, sal
来自员工
哪里萨尔> 2000或盐> 3000或盐> 4000;
While using all
SELECT empno, sal
FROM emp
WHERE sal > ALL (2000, 3000, 4000);
It will return result equivalent to query:
SELECT empno, sal
FROM emp
WHERE sal > 2000 AND sal > 3000 AND sal > 4000;
While using any
SELECT empno, sal
FROM emp
WHERE sal > ANY (2000, 3000, 4000);
Returns a result same as
SELECT empno, sal
FROM emp
WHERE sal > 2000 OR sal > 3000 OR sal > 4000;
IN - 很容易理解。查询应仅选择“IN”子句中指定的那些值。
现在,让我们通过查询来理解“ANY”。 ANY 表示它应该大于或小于列表中的任何值。
假设一个 Orders 表,其 OrderID 从 1 到 10
观察以下查询:
从订单中选择订单 ID
其中订单 ID < ANY (3,5,7)
上述查询的答案是:
订单ID
1,2,3,4,5,6
解释:查询表示查找小于任意指定值的 OrderID。因此数据库搜索并包含 OrderID,如下所示:
是 1<3- 是,因此包含 OrderID 1
是 2<3- 是,因此包含 OrderID 2
是 3<3- 否,是 3<5-是(因为 5 是指定值),因此包含 OrderID 3
是 4<3- 否,是 4<5 - 是,因此包含 OrderID 4
是 5<3- 否,是 5<5 - 否,是 5<7(因为 5 是指定值)- 是,因此包含 OrderID 5
是 6<3- 否,是 6<5-否,是 6<7-是,因此包含 OrderID 6
是 7<3- 否,是 7<5-否,是 7<7-否,因此不包括 OrderID 7,因为指定列表中没有更多值可供比较
是 8<3- 否,是 8<5-否,是 8<7-否,因此不包括 OrderID 8,因为指定列表中没有更多值可供比较
是 9<3-否,是 9<5-否,是 9<7-否,因此不包括 OrderID 9,因为指定列表中没有更多值可供比较
是 9<3- 否,是 9<5-否,是 9<7-否,因此不包括 OrderID 9,因为指定列表中没有更多值可供比较
对于大于
应用相同的逻辑
从订单中选择订单 ID
其中订单 ID > ANY (3,5,7)
上述查询的答案是:
订单ID
4,5,6,7,8,9,10
IN - It is easy to understand. The query should select only those values which are specified in 'IN' clause.
Now, let us understand 'ANY' with a query. ANY means it should be greater or less than any of the values in the list.
Assume a Orders table which has OrderID from 1 to 10
Observer the below query:
select OrderID from Orders
where OrderID < ANY (3,5,7)
The answer to above query is :
OrderID
1,2,3,4,5,6
Explanation :The query says find OrderIDs which are less than ANY of the specified values. So the database searches and includes OrderID as follows:
Is 1<3- Yes hence OrderID 1 is included
Is 2<3- Yes hence OrderID 2 is included
Is 3<3- No, is 3<5 -Yes (as 5 is specified value), hence OrderID 3 is included
Is 4<3- No, is 4<5 -Yes, hence OrderID 4 is included
Is 5<3- No, is 5<5 -No, is 5<7(as 5 is specified value)-Yes hence OrderID 5 is included
Is 6<3- No, is 6<5 -No, is 6<7-Yes hence OrderID 6 is included
Is 7<3- No, is 7<5 -No, is 7<7-No hence OrderID 7 is NOT included as no more values in specified list to compare
Is 8<3- No, is 8<5 -No, is 8<7-No hence OrderID 8 is NOT included as no more values in specified list to compare
Is 9<3- No, is 9<5 -No, is 9<7-No hence OrderID 9 is NOT included as no more values in specified list to compare
Is 9<3- No, is 9<5 -No, is 9<7-No hence OrderID 9 is NOT included as no more values in specified list to compare
Apply the same logic for greater than
select OrderID from Orders
where OrderID > ANY (3,5,7)
The answer to above query is :
OrderID
4,5,6,7,8,9,10
SQL SERVER 2008R2 中的任何和所有操作员。
使用 >以比较运算符为例,>ALL 表示大于每个值,换句话说,大于最大值。例如,>ALL(1,2,3)表示大于3。>ANY表示大于至少一个值,即大于最小值。所以>ANY(1,2,3)表示大于1。
同样,>ANY表示某行要满足外查询指定的条件,引入子查询的列中的值必须大于at子查询返回的值列表中的至少一个值。
ANY and ALL OPERATOR IN SQL SERVER 2008R2.
Using the > comparison operator as an example, >ALL means greater than every value--in other words, greater than the maximum value. For example, >ALL (1, 2, 3) means greater than 3. >ANY means greater than at least one value, that is, greater than the minimum. So >ANY (1, 2, 3) means greater than 1.
Similarly, >ANY means that for a row to satisfy the condition specified in the outer query, the value in the column that introduces the subquery must be greater than at least one of the values in the list of values returned by the subquery.
对于 ANY,您需要一个运算符:
而对于 IN,则不需要。它总是在测试是否相等。
With ANY, you need an operator:
With IN, you can't. It's always testing for equality.
= ANY 相当于 IN 运算符。 “<>、<、>、<= 或 >= 其中之一可以放在任何运算符之前。
请注意,<> ANY 运算符与 NOT IN 不同。
ANY 和 ALL 运算符与 WHERE 或 HAVING 子句一起使用。
如果任何子查询值满足条件,ANY 运算符将返回 true。
如果所有子查询值都满足条件,则 ALL 运算符返回 true。
= ANY is equivalent to IN operator. "<>, <, >, <=, or >=" one of them can be placed before ANY operator.
Note that the <> ANY operator is different from NOT IN.
The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
ANY 和 ALL 运算符与 WHERE 或 HAVING 子句一起使用。
如果任何子查询值满足条件,ANY 运算符将返回 true。
如果所有子查询值都满足条件,则 ALL 运算符返回 true。
The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
(in) 是一种特殊的运算符,用于从我们指定的值列表中逐个选取值。而 (any) 与 where 子句一起使用
(in) is a special kind of operator which is use to pick value one by one from list of values which we have specified.while (any) is use with where clause
当我们在某个集合上使用
IN
比较任何列值时,比如{value1,value2 ...}
那么列值必须存在于集合中,但在以下情况下ANY
我们这样比较:那么该值必须大于任意一个设定值。
如果是
ALL
,则该值必须大于集合中的所有值。
请参阅以下图片以更好地理解:
When we are comparing any column value using
IN
on some set, say{value1,value2 ...}
then the column value must be present in the set but in case ofANY
we compare like this:then the value must be greater than any one of the set value.
in case of
ALL
the value must be greater than all the values in the set.
Refer to the following images for better understanding: