SQL中IN和ANY运算符的区别

发布于 2024-09-19 13:25:13 字数 58 浏览 4 评论 0原文

SQL 中的 INANY 运算符有什么区别?

What is the difference between IN and ANY operators in SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

烟沫凡尘 2024-09-26 13:25:14
SQL>
SQL> -- Use the ANY operator in a WHERE clause to compare a value with any of the values in a list.
SQL>

SQL> -- 您必须在 ANY 之前放置 =、<>、<、>、<= 或 >= 运算符。

SQL> SELECT *
  2  FROM employee
  3  WHERE salary > ANY (2000, 3000, 4000);

对于 In 运算符

SQL> -- Use the IN operator in a WHERE clause to compare a value with any of the values in a list.
SQL> SELECT *
  2  FROM employee
  3  WHERE salary IN (2000, 3000, 4000);

但对于 IN 运算符,您不能使用 = 、<>、<、>、<= 或 >=

SQL>
SQL> -- Use the ANY operator in a WHERE clause to compare a value with any of the values in a list.
SQL>

SQL> -- You must place an =, <>, <, >, <=, or >= operator before ANY.

SQL> SELECT *
  2  FROM employee
  3  WHERE salary > ANY (2000, 3000, 4000);

For In Operator

SQL> -- Use the IN operator in a WHERE clause to compare a value with any of the values in a list.
SQL> SELECT *
  2  FROM employee
  3  WHERE salary IN (2000, 3000, 4000);

But with the IN operator you cannot use =, <>, <, >, <=, or >=

亢潮 2024-09-26 13:25:14

IN - 等于列表中的任何内容

ANY - 将值与每个值进行比较由子查询返回。

ALL - 将值与子查询返回的每个值进行比较。

例如:

IN:

显示与部门投资最少的薪资相匹配的所有员工的详细信息?

 Select Ename, Sal, Deptno 
 from Emp 
 Where Sal IN (Select Min(Sal) 
               From Emp 
               Group By Deptno);

任何:

<代码>< ANY 表示小于列表中的最大值。

获取收入低于最高收入经理的所有员工的详细信息?

 Select Empno, Ename, Job, Sal 
 From Emp
 Where Sal < Any (Select Distinct MGR 
                  From Emp);

<代码>> ANY 表示大于列表中的最小值。

获取第 10 部门所有收入高于最低工资的员工的详细信息?

 Select Empno, Ename, Job, Sal 
 From Emp
 Where Sal > Any (Select Min(Sal) 
                  From Emp 
                  Where Deptno 10);

= ANY 相当于 in 运算符。

注意:也可以使用 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?

 Select Ename, Sal, Deptno 
 from Emp 
 Where Sal IN (Select Min(Sal) 
               From Emp 
               Group By Deptno);

ANY:

< ANY means less than the maximum value in the list.

Get the details of all employees who are earning less than the highest earning manager?

 Select Empno, Ename, Job, Sal 
 From Emp
 Where Sal < Any (Select Distinct MGR 
                  From Emp);

> ANY means more than the minimum value in the list.

Get the details of all employees who are earning more than the least paid in Department 10?

 Select Empno, Ename, Job, Sal 
 From Emp
 Where Sal > Any (Select Min(Sal) 
                  From Emp 
                  Where Deptno 10);

= ANY is equivalent to in operator.

Note: SOME can also be used instead of ANY.

情丝乱 2024-09-26 13:25:14

也许为了更好地理解,这两个条件是等效的。使用哪一个是一个品味问题(前提是 RDBMS 支持两者)

... WHERE x IN (SELECT Y FROM THE_TABLE)  
... WHERE x =ANY (SELECT Y FROM THE_TABLE) 

... WHERE x NOT IN (SELECT Y FROM THE_TABLE) 
... WHERE x <>ALL (SELECT Y FROM THE_TABLE) 

实际上我个人的习惯是使用 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)

... WHERE x IN (SELECT Y FROM THE_TABLE)  
... WHERE x =ANY (SELECT Y FROM THE_TABLE) 

and these also

... WHERE x NOT IN (SELECT Y FROM THE_TABLE) 
... WHERE x <>ALL (SELECT Y FROM THE_TABLE) 

Actually my personal habit is to use IN for list expression (like WHERE x IN (2,4,6,8) and =ANY, resp. <>ALL for sub-queries.

情域 2024-09-26 13:25:14

使用 all 时

SELECT empno, sal
来自员工
哪里萨尔>全部(2000、3000、4000);

 EMPNO        SAL

  7839       5000

它将返回相当于查询的结果:

SELECT empno, sal
来自员工
哪里萨尔> 2000年和萨尔> 3000 AND 盐> 4000;

使用任意时

SELECT empno, sal
来自员工
哪里萨尔>任意(2000、3000、4000);

 EMPNO        SAL

  7566       2975
  7698       2850
  7782       2450
  7788       3000
  7839       5000
  7902       3000

相同的结果

返回与SELECT empno, sal
来自员工
哪里萨尔> 2000或盐> 3000或盐> 4000;

While using all

SELECT empno, sal
FROM emp
WHERE sal > ALL (2000, 3000, 4000);

 EMPNO        SAL

  7839       5000

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);

 EMPNO        SAL

  7566       2975
  7698       2850
  7782       2450
  7788       3000
  7839       5000
  7902       3000

Returns a result same as

SELECT empno, sal
FROM emp
WHERE sal > 2000 OR sal > 3000 OR sal > 4000;

看透却不说透 2024-09-26 13:25:14

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

想念有你 2024-09-26 13:25:14

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.

烟雨凡馨 2024-09-26 13:25:14

对于 ANY,您需要一个运算符:

WHERE X > ANY (SELECT Y FROM Z)

而对于 IN,则不需要。它总是在测试是否相等。

With ANY, you need an operator:

WHERE X > ANY (SELECT Y FROM Z)

With IN, you can't. It's always testing for equality.

铁轨上的流浪者 2024-09-26 13:25:14

= 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.

过气美图社 2024-09-26 13:25:14

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.

贱人配狗天长地久 2024-09-26 13:25:14

(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

如梦初醒的夏天 2024-09-26 13:25:14

当我们在某个集合上使用 IN 比较任何列值时,比如 {value1,value2 ...} 那么列值必须存在于集合中,但在以下情况下ANY 我们这样比较:

col.value > ANY ({value1,value2,...})

那么该值必须大于任意一个设定值。

如果是ALL,则

col.value > ALL({value1,value2,...})

该值必须大于集合中的所有值。

请参阅以下图片以更好地理解:

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 of ANY we compare like this:

col.value > ANY ({value1,value2,...})

then the value must be greater than any one of the set value.

in case of ALL

col.value > ALL({value1,value2,...})

the value must be greater than all the values in the set.

Refer to the following images for better understanding:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文