2010-08-30 39 views
1

在mysql中,我想在同一個表格單元上做兩個獨特的LEFT JOIN。如何在同一個表格單元上做2個獨特的LEFT JOIN?

我有兩個表。

一個表格列出了單獨的客戶端,併爲每個客戶端有一個clientNoteID和staffNoteID條目。 clientNoteID和staffNoteID都是notesTable中note存儲的唯一noteID的整數引用。

clientsTable:

clientID | clientName | clientNoteID | staffNoteID 

notesTable:

noteID | note 

我希望能夠在notesTable由clientNoteID和引用的筆記中所涉及的說明兩者的選擇出由staffNoteID提供。

我沒有看到任何方式別名LEFT JOIN,如:

SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID 
LEFT JOIN notes on clientTable.clientNotesID = notes.noteID 
LEFT JOIN notes on clientTable.staffNoteID = notes.noteID as staffNote 

(不,我認爲,真正使太大的意義)

所以,我怎麼能查詢,使我可以在最後打印出來:

clientName | clientNote | staffNote 

回答

2

當你加入一個表時,alas必須在表名之後,而不是在聯接條件之後。試試這個:

SELECT clientsTable.clientName, n1.note AS clientNote, n2.note AS staffNote 
FROM clientsTable 
LEFT JOIN notes AS n1 ON clientTable.clientNotesID = n1.noteID 
LEFT JOIN notes AS n2 ON clientTable.staffNoteID = n2.noteID 
+0

感謝這麼多,漂亮的工作表 – salonMonsters 2010-08-30 23:12:56

1

你需要自己別名

SELECT FROM clientsTable clientsTable.clientID, clientsTable.clientName, clientsTable.clientNoteID, clientsTable.stylistNoteID 
LEFT JOIN notes a on clientTable.clientNotesID = a.noteID 
LEFT JOIN notes b on clientTable.staffNoteID = b.noteID 
1
SELECT CT.clientName, N1.note AS clientNote, N2.note AS staffNote 
FROM clientsTable CT 
LEFT JOIN notes N1 on CT.clientNotesID = N1.noteID 
LEFT JOIN notes N2 on CT.staffNoteID = N2.noteID