2

您好我正在尝试合并两个表。

我的 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)

4

3 回答 3

3

按照建议添加括号或更改缩写...

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
于 2012-05-23T12:01:18.747 回答
2

只需在您的选择查询周围添加一些括号:

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

例如,参见文档中的示例

于 2012-05-23T09:18:29.203 回答
-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)

但是,您得到的错误似乎是临时表限制。

于 2012-05-23T09:18:07.730 回答