簡體   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