2017-03-18 80 views
2

我在SQL數據庫三個表SQL - 全外連接

enter image description here

我需要加入這個三個表讓臺這樣

enter image description here

我想聯接三個表來獲得整個第一,第三和第二(其中排序爲2)的所有數據表

我試試這個查詢

select table1.Item, table1.Location, table1.Type, 
     table2.Item, table2.Location, table2.Type,table2.Sort 
     table3.Item, table3.Location, table3.Type 
    from table1 
full outer join table2 
    on table1.Item = table2.Item 
    and table1.Location = table2.Location 
    and table1.Type = table2.Type 
    and table2.Sort = '2' 
full outer join table3 
    on table1.Item = table3.Item 
    and table1.Location = table3.Location 
    and table1.Type = table3.Type 

但我只得到這個表

enter image description here

如何使查詢來獲取表我想與所有的組合?

有什麼想法?

+0

你在哪個RDMS上? – trincot

+0

微軟SQL服務器 – dev

+0

可以請你發表你的表格和代碼作爲格式文本,而不是圖像 – mansi

回答

3
select table1.Item, table1.Location, table1.Type, 
     table2.Item, table2.Location, table2.Type, table2.Sort, 
     table3.Item, table3.Location, table3.Type 
from table1 
    full outer join table3 
    on table1.Item = table3.Item 
    and table1.Location = table3.Location 
    and table1.Type = table3.Type 
    full outer join table2 
    on (table1.Item = table2.Item 
    and table1.Location = table2.Location 
    and table1.Type = table2.Type 
    and table2.Sort = '2' 
    ) 
    or (table3.Item = table2.Item 
    and table3.Location = table2.Location 
    and table3.Type = table2.Type 
    and table2.Sort = '2' 
    ) 
where (table2.Sort = '2' or table2.Sort is null) 

rextester演示:http://rextester.com/MNW25175

結果

+------+----------+-------+------+----------+-------+------+------+----------+-------+ 
| Item | Location | Type | Item | Location | Type | Sort | Item | Location | Type | 
+------+----------+-------+------+----------+-------+------+------+----------+-------+ 
| 123 | A  | small | NULL | NULL  | NULL | NULL | 123 | A  | small | 
| NULL | NULL  | NULL | 123 | A  | big | 2 | 123 | A  | big | 
| NULL | NULL  | NULL | 123 | B  | small | 2 | NULL | NULL  | NULL | 
+------+----------+-------+------+----------+-------+------+------+----------+-------+ 

這也適用,但只是因爲table2有一個匹配的table1所有行,這可能不適合你的實際使用情況:

select table1.Item, table1.Location, table1.Type, 
     table2.Item, table2.Location, table2.Type, table2.Sort, 
     table3.Item, table3.Location, table3.Type 
from table1 
    full outer join table3 
    on table1.Item = table3.Item 
    and table1.Location = table3.Location 
    and table1.Type = table3.Type 
    full outer join table2 
    on table3.Item = table2.Item 
    and table3.Location = table2.Location 
    and table3.Type = table2.Type 
    and table2.Sort = '2' 
where (table2.Sort = '2' or table2.Sort is null) 
+0

Thaks很多.. :) – dev

+0

@dev快樂的幫助! – SqlZim

1

使用LEFT OUTER JOIN:

SELECT * FROM Table 2 
LEFT OUTER JOIN Table 1 ON table1.Item = table2.Item AND table1.Location = 
        table2.Location AND table1.Type = table2.Type and 
        table2.Sort = '2' 
LEFT OUTER JOIN Table 3 ON table1.Item = table3.Item 
       and table1.Location = table3.Location 
       and table1.Type = table3.Type 
+0

但我可以在第三個表中獲得比第二個更多的數據...這個代碼比不工作:( – dev

+0

但你在表2中有更多的行比表3 – Mansoor

5

你的做法是正確的,你就錯過是你的第二個加入審覈規定使用​​3210。

第一outer join將返回一組行的一些null值在裏面,如果你使用的第三join規定 - 這些價值觀,病情會不會滿意。

可以解決使用​​3210這個問題,將使用第二個參數,如果第一個是null

select table1.Item, table1.Location, table1.Type, 
     table2.Item, table2.Location, table2.Type,table2.Sort 
     table3.Item, table3.Location, table3.Type 
from table1 
full outer join table2 
on  table1.Item = table2.Item and 
     table1.Location = table2.Location and 
     table1.Type = table2.Type 
full outer join table3 
on  coalesce(table1.Item, table2.Item) = table3.Item and 
     coalesce(table1.Location, table2.Location) = table3.Location and 
     coalesce(table1.Type, table2.Type) = table3.Type 
where coalesce(table2.Sort, 2) = 2 
+2

我認爲你需要添加'where coalesce(table2.Sort,'2')='2'',否則你可能會在輸出中得到其他'Sort'值 – trincot

+0

正確,回答編輯: ) –

1

你得到通過以下查詢

SELECT d.item, d.location, d.[type], 
    s.item, s.location, s.[type], s.sort, 
    h.item, h.location, h.[type] 
FROM tblData d 
    FULL OUTER JOIN tblSearchData s 
     ON d.item = s.item 
     AND d.location = s.location 
     AND d.[type] = s.[type] 
    FULL OUTER JOIN tblHelpData h 
     ON (s.item = h.item OR d.item=h.item) 
     AND (s.location = h.location OR d.location=h.location) 
     AND (s.[type] = h.[type] OR d.[type]=h.[type]) 
WHERE s.sort=2 OR s.sort IS NULL 
所需的結果