2015-09-27 54 views
2
select deptno 
from emp2 
MINUS 
select deptno 
from dpt 
order by deptno; 

上述查询返回=无数据实测值SQL MINUS操作者

然而,

select deptno,ename 
from emp2 
MINUS 
select deptno,dname 
from dpt 
order by deptno; 

返回所有DEPTNO和ENAME字段值。

您能否解释为什么我会在第二个查询中使用MINUS运算符来获取deptno字段的所有值?

legend: 
emp is employee table and dpt is department table, 
ename is employee name -belonging to emp, 
dname is department name -belonging to dpt, 
deptno is department no. -common to both 

回答

6

在第一个查询没有表之间没有什么不同deptno,第二你有相同的deptno,但名称不同

想想看这样的:

查询1 :

select deptno 
from emp2 
MINUS 
select deptno 
from dpt 
order by deptno; 

实施例:

[1,2,3,4] - [1,2,3,4] = empty 

问题2:

select deptno,ename 
from emp2 
MINUS 
select dept no,dname 
from dpt 
order by deptno; 

实施例:

[(1, 'a'),(2, 'b'),(3, 'c'),(4, 'd')] - 
[(1, 'z'),(2, 'x'),(3, 'u'),(4, 'w')] = 
[(1, 'a'),(2, 'b'),(3, 'c'),(4, 'd')] 

MINUS

MINUS操作者,它返回由第一 查询,但不被返回唯一行第二个

+1

非常感谢你,现在让我完全感觉:) –