2009-09-16 83 views
0
mysql> select count(id) total from applicants; 
+-------+ 
| total | 
+-------+ 
|  0 | 
+-------+ 
1 row in set (0.00 sec) 

mysql> select count(id) total from jobs; 
+-------+ 
| total | 
+-------+ 
|  0 | 
+-------+ 
1 row in set (0.00 sec) 

mysql> select count(id) total from applicants union select count(id) total from 
jobs; 
+-------+ 
| total | 
+-------+ 
|  0 | 
+-------+ 
1 row in set (0.00 sec) 

mysql> 

是不是應該有兩個記錄「0」作爲它的值?爲什麼只有一個記錄結合後?

回答

5

Union All將給出兩個聲明。聯盟嘗試組合條目,或者更確切地說,刪除重複的行。

1

UNION刪除重複項。嘗試使用UNION ALL。

3

MySQL manual報價:工會

的默認行爲是重複的行從 結果刪除。可選的DISTINCT 關鍵字除 默認值外沒有任何效果,因爲它還指定 刪除重複行。使用 可選ALL關鍵字,不會發生重複行 刪除,並且結果 包括SELECT語句的所有 中的所有匹配行。

這意味着,只有UNION,如果兩行具有相同的值,其中一人將不予退還:

mysql> select 1 union select 1; 
+---+ 
| 1 | 
+---+ 
| 1 | 
+---+ 
1 row in set (0.00 sec) 

但是,如果使用UNION ALL,則不刪除重複:

mysql> select 1 union all select 1; 
+---+ 
| 1 | 
+---+ 
| 1 | 
| 1 | 
+---+ 
2 rows in set (0.00 sec) 
相關問題