【发布时间】:2017-06-09 04:24:11
【问题描述】:
我目前必须在两个表(右外+左外)上模拟完全外连接,并使用联合来消除重复项。
我想知道,因为我有很多表要这样做,并且我想最终得到一个表,有没有更好的方法来做到这一点。
这是我目前正在做的:
create table `table+left` as(
select table1.col1, table1.col2, table1.col3, table2.col2 as `alias`
from table1
left outer join table2
on table1.col1 = table2.col1
);
create table `table+right` as(
select table1.col1, table1.col2, table1.col3, table2.col2 as `alias`
from table1
right outer join table2
on table1.col1 = table2.col1
);
create table `table1+table2` as
select * from `table+left`
union
select * from `table+right`;
【问题讨论】:
-
我只是松了一口气,我不必使用这个命名策略:-|
-
你想完成什么?添加您想要实现的示例数据和结果。
-
创建表 'table+right' 中的选择是否正确 - 看起来你会在 table1 中没有匹配的 table1.col1、table1.col2、table1.col3 得到空值?
-
@P.Salmon 是的,创建表有效。
-
@SirajulHaq 我想在第一个表末尾的列上追加,然后删除重复项。
标签: mysql join union full-outer-join