繁体   English   中英

将两个表与一个不同的列联合起来

[英]Union two tables with one different column

除了一列,我有两个几乎相同的表。

table1 
     id name amount
     1  nm1  15
     2  nm2  20

table1 
     id name amt
     1  nm1  15
     2  nm2  20

现在我有其他连接,但我想避免将其全部连接两次,而是使用更简单的 sql 代码。

目前我必须做两次:

select t1.id, t1.name, t1.amount from table1 t1 
       left outer join.......
  union all 
select t2.id, t2.name, t2.amt as amount from table2 t2 
       left outer join.......

我想要一些类似的东西:

select t1.id, t1.name, t1.amount from (select * from table1 union all select * from table2) t1

但是这一列“金额”阻止了它。

有没有办法处理它?

join之前做union all

select t.id, t.name, t.amount, . . .
from (select t1.* from table1 t1 union all
      select t2.* from table2 t2
     ) t left outer join.......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM