2013-04-20 25 views
1

我需要在兩個表上執行FULL OUTER JOIN,我正試圖在MySQL中使用LEFT JOIN/RIGHT JOIN/UNION ALL技術來實現它。MySQL UNION只包括第一個表

這裏是原始表:

giving_totals: 
+--------------+---------------+-------------+ 
| country_iso2 | total_given | supersector | 
+--------------+---------------+-------------+ 
| AE   | 1396986989.02 |   3 | 
| AE   | 596757809.20 |   4 | 
| AE   | 551810209.87 |   5 | 
| AE   | 25898255.77 |   7 | 
| AE   |  32817.63 |   9 | 
... 
+--------------+---------------+-------------+ 

receiving_totals: 
+--------------+----------------+-------------+ 
| country_iso2 | total_received | supersector | 
+--------------+----------------+-------------+ 
| AE   | 34759000.00 |   3 | 
| AE   |  148793.82 |   7 | 
| AE   |   734.30 |   9 | 
| AF   | 6594479965.85 |   1 | 
| AF   | 2559712971.26 |   2 | 
+--------------+----------------+-------------+ 

我想結果表中有每個國家的每個超羣代碼的一個條目,即使它沒有給予或接受金錢爲扇區(這是從AidData項目數據集的情況下,任何人都很熟悉。)我想通過做一個左連接(獲得所有給予條目)和右連接(獲取所有接收條目)的聯合來完成此操作。以下是我嘗試的查詢:

SELECT g.country_iso2 AS country_iso2, g.total_given AS `total_given`,R.total_received AS `total_received`,g.supersector AS `supersector` 
FROM (`giving_totals` `g` 
LEFT JOIN `receiving_totals` `r` 
ON(((g.country_iso2 = r.country_iso2) 
AND (g.supersector = r.supersector)))) 

UNION ALL 

SELECT g.country_iso2 AS country_iso2, g.total_given AS `total_given`,R.total_received AS `total_received`,g.supersector AS `supersector` 
FROM (`giving_totals` `g` 
RIGHT JOIN `receiving_totals` `r` 
ON(((g.country_iso2 = r.country_iso2) 
AND (g.supersector = r.supersector)))) 

但是這隻返回第一個jo無論我是否先將右側或左側連接起來。我想我可能會誤解UNION的運作,因爲每個人的加入都會返回我的預期。任何幫助一如既往地受到讚賞。

+0

在第二次選擇工會時使用您加入的表中的表別名,否則您將獲得NULL country_iso2和supersector。如果您不想查看包含給定和接收兩次的行,請使用UNION而不是UNION ALL。除此之外,查詢沒有任何問題。 – piotrm 2013-04-20 22:28:14

回答

0

這裏是做一個full outer join的替代方法:

SELECT driver.country_iso2 AS country_iso2, 
     g.total_given AS `total_given`, 
     R.total_received AS `total_received`, 
     driver.supersector AS `supersector` 
from ((select distinct country_iso2, supersector 
     from giving_totals 
    ) union 
     (select distinct country_iso2, supersector 
     from receiving_totals 
    ) 
    ) driver left outer join 
    giving_totals gt 
    on gt.country_iso2 = driver.country_iso2 and 
     gt.supersector = driver.country_iso2 left outer join 
    receiving_totals rt 
    on rt.country_iso2 = driver.country_iso2 and 
     rt.supersector = driver.country_iso2 

那就是,做工會作爲一個子查詢來獲取所有你感興趣的組合那麼你可以做一個left outer join到該表。

您的問題的原因是第二個查詢中的別名。你可以試試這個:

SELECT r.country_iso2 AS country_iso2, g.total_given AS `total_given`,R.total_received AS `total_received`,r.supersector AS `supersector` 
FROM (`giving_totals` `g` 
RIGHT JOIN `receiving_totals` `r` 
ON(((g.country_iso2 = r.country_iso2) 
AND (g.supersector = r.supersector)))) 

原始窗體對於這些值將具有NULL值。

+0

使用你的第一個查詢,total_given和total_received字段是NULL(我現在看到你在帖子的底部提到了)。但是,如何使用第二個查詢來獲取實際值?自己運行該查詢與我原來的查詢具有相同的效果。 – Matt 2013-04-20 21:05:47

+0

@Matt。 。 。出現不匹配時,您將獲得NULL值。只有一個表中存在一些iso/supersector的值。另一個表中的值將爲NULL。 – 2013-04-21 00:37:42

+0

我仍然試圖解決這個問題,但我不明白爲什麼我試過的聯盟不起作用。加在一起,左加入和右加入具有所有可能的組合,因此將它們放入一張桌子應該將它們放在一起。但聯盟並沒有像現在這樣工作。也許我不理解你的評論,但我仍然不明白爲什麼我原來的查詢不起作用。 – Matt 2013-04-22 23:12:33