[英]Display non-matching records in sql
我有兩個表..Table1具有product_id和產品來源,而table2具有product_id和項。
Table1 data
------------
Product_id Product_source
101 CRM
102 CNT
Table2 data
-----------------
Product_id item
101 item1
101 item2
我希望結果為:
Product_source item
CRM item1
CRM item2
CNT null
select product_source , item
from table1 , tabl2
where product_id(+)=tab2.product_id
我也嘗試了外部連接,但沒有得到結果。 請協助。
您需要左加入
SELECT
T1.product_source
,T2.item
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.product_id = T2.product_id
您必須執行左外部聯接才能獲得此結果:
select product_source, item
from table1
left outer join table2 on table1.product_id = table2.product_id
表格1:
Product_id | Product_source |
101 | CRM |
102 | CNT |
表2:
Product_id | item |
101 | item1 |
101 | item2 |
我希望結果為:
Product_source | item |
CRM | item1 |
CRM | item2 |
CNT | null |
既然如此,我想您會想要以下內容:
SELECT
t1.product_source, t2.item
FROM
table1 t1
LEFT OUTER JOIN table2 t2
ON
t1.product_id = t2.product_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.