![](/img/trans.png)
[英]UNION two tables with different columns, without duplicates, and update a field on union
[英]Union two tables with different columns and display null in the output
我有兩個表customer_tbl和Suppliers_tbl,如果列不同,如何合並這兩個表?
customer_tbl:
Suppliers_tbl:
這應該是輸出:
我嘗試使用工會和左聯接,但它有一個錯誤。
這是我的代碼
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.