SQL> conn scott/tiger
已连接。
SQL> select ename,sal from emp;
ENAME SAL
---------- ----------
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 4000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
已选择14行。
SQL> select ename,sal from emp where sal<2000
2 minus
3 select ename,sal from emp where sal>1000
4 /
ENAME SAL
---------- ----------
JAMES 950
SMITH 800
SQL> select ename,sal from ( select empno,ename,sal from emp where sal<2000) e1
2 where not exists(select * from emp where empno=e1.empno and sal>1000)
3 /
ENAME SAL
---------- ----------
SMITH 800
JAMES 950
如果sal列存在null值,则因为在第一行的子查询中已经被过滤掉,所以不会出现到结果中:
SQL> update emp set sal=null where empno=7369;
已更新 1 行。
SQL> select ename,sal from ( select empno,ename,sal from emp where sal<2000) e1
2 where not exists(select * from emp where empno=e1.empno and sal>1000)
3 /
ENAME SAL
---------- ----------
JAMES 950
SQL> select ename,sal from ( select empno,ename,sal from emp where sal<2000 or sal is null) e1
2 where not exists(select * from emp where empno=e1.empno and sal>1000) 3 /
ENAME SAL
---------- ----------
SMITH
JAMES 950
except是SQL99的标准语法,SQL Server 2005支持except,等同于Oracle中的minus,而Oracle直到11g还不支持except。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/37724/viewspace-152652/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/37724/viewspace-152652/
本文通过具体示例展示了如何使用SQL进行数据筛选和对比操作,包括基本的SELECT语句用法,以及MINUS和EXISTS子句的应用。还特别提到了NULL值处理的方法,并对比了不同数据库管理系统间的语法差异。
2903

被折叠的 条评论
为什么被折叠?



