2016-12-16 64 views
1

所以我有一個加入,它的工作原理,但它並沒有顯示我想要的所有數據。目前我想查看device_interface.c1中的每一行,如果device_interface.c1 = device_inventory.c1那麼我想在列中看到。現在我只看到device_interface.c1 = device_inventory.c1postgresql - 在匹配上加入表,但也不匹配?

下面是我的查詢:

SELECT 
    device_info.pk, 
    device_interface.fk, 
    device_interface.c1, 
    device_interface.c2, 
    device_inventory.c1 
FROM 
    "test".device_info, 
    "test".device_interface, 
    "test".device_inventory 
WHERE 
    device_info.pk = device_interface.fk AND 
    device_info.pk = device_inventory.fk AND 
    device_interface.c1 = device_inventory.c1 AND 
    device_interface.c2 = 'Physical' 

示例輸出:期望的輸出的

device_info.pk device_interface.c1 device_inventory.c1 
1      1      1 
1      3      3 
1      95     95 
2      55     55 
2      634     634 

實施例:

device_info.pk device_interface.c1 device_inventory.c1 
1      1      1 
1      2   
1      3      3 
1      4 
1      5 
1      6 
1      95     95 
2      55     55 
2      56 
2      57 
2      634     634 
+0

您正在尋找的是左連接。對不起,我沒有時間回答,但看看這篇關於[Visual SQL Representation of SQL Joins]的文章(https://www.codeproject.com/articles/33052/visual-representation-of-sql-joins )。 – Schwern

+1

謝謝你我會通過 – Aladine

回答

2

left join應該做的伎倆:

SELECT 
    device_info.pk, 
    device_interface.fk, 
    device_interface.c1, 
    device_interface.c2, 
    device_inventory.c1 
FROM 
    "test".device_info 
LEFT JOIN 
    "test".device_interface 
ON 
    device_info.pk = device_interface.fk 
LEFT JOIN 
    "test".device_inventory 
ON 
    device_info.pk = device_inventory.fk AND 
    device_interface.c1 = device_inventory.c1 
WHERE 
    device_interface.c2 = 'Physical' 
+2

閱讀這個謝謝你們我在這個新的和這將有助於很多! – Aladine