2015-09-27 84 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

非常感謝你,現在讓我完全感覺:) –