官方参考: http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions002.htm#sthref1908
以scott/tiger 的emp表作为示例:
ANY:类似于OR操作符。大于任何值即大于最小的那个值
scott@TEST0924> SELECT empno, sal
2 FROM emp
3 WHERE sal > ANY (2000, 3000);
EMPNO SAL
---------- ----------
7566 2975
7698 2850
7782 2450
7788 3000
7839 5000
7902 3000
6 rows selected.
等价于大于任何值
scott@TEST0924> SELECT empno, sal
2 FROM emp
3 WHERE sal > 2000 or sal>3000
4 ;
EMPNO SAL
---------- ----------
7566 2975
7698 2850
7782 2450
7788 3000
7839 5000
7902 3000
6 rows selected.
等价于大于最小值
scott@TEST0924> SELECT empno, sal
2 FROM emp
3 WHERE sal > 2000 ;
EMPNO SAL
---------- ----------
7566 2975
7698 2850
7782 2450
7788 3000
7839 5000
7902 3000
6 rows selected.
scott@TEST0924> SELECT e2.sal
2 FROM emp e2
3 WHERE e2.deptno = 10;
SAL
----------
2450
5000
1300
scott@TEST0924> SELECT e1.empno, e1.sal
2 FROM emp e1
3 WHERE e1.sal > ANY (SELECT e2.sal
4 FROM emp e2
5 WHERE e2.deptno = 10);
EMPNO SAL
---------- ----------
7839 5000
7902 3000
7788 3000
7566 2975
7698 2850
7782 2450
7499 1600
7844 1500
8 rows selected.
即等价于大于最小值
scott@TEST0924> SELECT e1.empno, e1.sal
2 FROM emp e1
3 WHERE e1.sal >1300
4 ;
也等价与
scott@TEST0924> SELECT e1.empno, e1.sal
2 FROM emp e1
3 WHERE EXISTS (SELECT e2.sal
4 FROM emp e2
5 WHERE e2.deptno = 10
6 AND e1.sal > e2.sal);
EMPNO SAL
---------- ----------
7839 5000
7902 3000
7788 3000
7566 2975
7698 2850
7782 2450
7499 1600
7844 1500
8 rows selected.
如果any后面的子查询返回非0行,则
- "x = ANY (...)": The value must match one or more values in the list to evaluate to TRUE.至少匹配一个值
- "x != ANY (...)": The value must not match one or more values in the list to evaluate to TRUE.一个值都不匹配
- "x > ANY (...)": The value must be greater than the smallest value in the list to evaluate to TRUE.大于最小值
- "x < ANY (...)": The value must be smaller than the biggest value in the list to evaluate to TRUE.小于最大值
- "x >= ANY (...)": The value must be greater than or equal to the smallest value in the list to evaluate to TRUE.大于等于最小值
- "x <= ANY (...)": The value must be smaller than or equal to the biggest value in the list to evaluate to TRUE.小于等于最大值
如果any后面的子查询返回为0行
此查询没有返回值
scott@TEST0924> SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100;
no rows selected
则以下any都没有返回值
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal=ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal!=ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal > ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal <ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal >=ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
scott@TEST0924> SELECT e1.empno, e1.sal FROM emp e1
2 WHERE e1.sal <=ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100);
no rows selected
ALL比较复杂参考: http://blog.csdn.net/rlhua/article/details/12006433