将两个表与一个不同的列合并
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.......
除了一列外,我有两个几乎相同的表。
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.......