2012-05-23 129 views
0

嗨即時嘗試合併兩個表。mysql union to not working

我有ids,只存在於左表中,我有隻存在於右表中的ids。我想得到所有ID的一張桌子。我想:

select * from temp_hours as a 
    left outer join temp_orders as o on a.proj_id = o.proj_id 
union 
select * from temp_hours as a 
    right outer join temp_orders as o on a.proj_id = o.proj_id 

,但我得到的錯誤:

Can't reopen table: 'a' 

任何想法如何,我可以做到這一點?服務器版本:5.0.51a-24 + lenny4(Debian的)

+0

嘗試把兩個SQL查詢中'()'。比如'(query1)union(query2)' – Sumant

+0

或者在第二個select中使用不同的別名。 – symcbean

+0

不知道這是否有助於你的情況,我意識到這是非常古老的,但也許這會幫助其他人,因爲它對我有用。 Union默認使用DISTINCT。如果工會提取相同的信息,它將自動僅顯示新的DISTINCT信息。我不得不爲每個選擇添加一些獨特的內容,以使其正確顯示。 SELECT列,1 FROM ... UNION SELECT列,2 FROM ...更多信息:http://forums.mysql.com/read.php?10,220906,220926#msg-220926 – nwolybug

回答

0

添加括號的建議或改變縮寫...

select * from temp_hours as a 
    left outer join temp_orders as ao on a.proj_id = ao.proj_id 
union 
select * from temp_hours as b 
    right outer join temp_orders as bo on b.proj_id = bo.proj_id 
0

你需要括號爲每一個選擇

(select * from temp_hours as a 
    left outer join temp_orders as o on a.proj_id = o.proj_id) 
union 
(select * from temp_hours as a 
    right outer join temp_orders as o on a.proj_id = o.proj_id) 

但是,錯誤你似乎temporary table limitation.

0

只需添加在你的SELECT查詢一些括號:

(select * from temp_hours as a 
    left outer join temp_orders as o on a.proj_id = o.proj_id) 
union 
(select * from temp_hours as a 
    right outer join temp_orders as o on a.proj_id = o.proj_id) 

例如,參見docu中的示例。

+0

謝謝,但我仍然得到同樣的錯誤。即時通訊使用臨時表,可能是一個問題? – user1195745

+0

在同一查詢中不能多次引用TEMPORARY表 - 發現它 – user1195745