簡體   English   中英

合並兩個具有不同列的表,並在輸出中顯示null

[英]Union two tables with different columns and display null in the output

我有兩個表customer_tbl和Suppliers_tbl,如果列不同,如何合並這兩個表?

customer_tbl:

http://imgur.com/a/0ATK6

Suppliers_tbl:

http://imgur.com/a/6hj3t

這應該是輸出:

http://imgur.com/a/0vtiX

我嘗試使用工會和左聯接,但它有一個錯誤。

這是我的代碼

select customerid, customername,
       contactname, address,
       city, postalcode, country 
from customer_tbl
left join (select supplierid as customername, 
                  address, city,country 
            from suppliers_tbl) 
  on customertbl_customername = suppliers_tbl.supplierod as customername;

對於聯合,您可以為匹配編號和類型添加空值

select
     customerid
     ,customername
     ,contactname
     ,address
     ,city
     ,country 
     ,postalcode
     from customer_tbl
union 
select 
     supplierid
    ,suppliername
    ,contactname
    ,address
    ,city
    ,country 
    , null
from suppliers_tbl

在您的示例中,您有一些無法使用的列,因此您可以對這些列使用null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM