2013-05-29 124 views
0

我有這2個表SQL兩人在另一個表中一個表引用同一列的列

tbl_link 

    pID | fID_a | fID_b | link_desc 
    1 | 1 | 2 | aa + bb 

tbl_structure 

    pID | desc 
    1 | a 
    2 | b 

fID_a和fID_b外鍵爲PID在tbl_structure fID_a不允許NULL值,而fID_b確實

我想查詢結構時檢索結構2的遞減1

此刻我的SQL查詢看起來像這樣

SELECT a.link_desc, tbl_structure.desc FROM tbl_strukture 
LEFT JOIN tbl_link as a ON tbl_structure.pID = a.fID_a 
LEFT JOIN tbl_link as b ON tbl_structure.pID = b.fID_b 
WHERE tbl_structure.pID = 1 

但我只能得到與pID 1結構的desc!

感謝您的幫助!

回答

2

你在找嗎?

SELECT l.link_desc, 
     s.[desc] description1, 
     s2.[desc] description2 
    FROM tbl_link l LEFT JOIN tbl_structure s 
    ON l.fID_a = s.pID LEFT JOIN tbl_structure s2 
    ON l.fID_b = s2.pID 
WHERE s.pID = 1 

SQLFiddle(SQL Server)的

SQLFiddle(MySQL的)

+0

...其中s1.pID = 1和它的作品!謝謝! – Markus

+0

@MarkusHölzner不客氣。很高興幫助:) – peterm

相關問題