mysql联合不起作用

时间:2012-05-23 09:15:03

标签: mysql join merge union

您好我试图合并两个表。

我的id只存在于左表中,而且我的id只存在于右表中。我希望得到一张包含所有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)

3 个答案:

答案 0 :(得分: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)

请参阅docu中的示例。

答案 1 :(得分: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

答案 2 :(得分:-1)

每个选择都需要括号

(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.