在mysql中,我想在同一个表格单元格上做两个独特的LEFT JOIN。
我有两张桌子。
一个表列出了各个客户端,并为每个客户端提供了clientNoteID和staffNoteID条目。 clientNoteID和staffNoteID都是notesTable中笔记存储的唯一noteID的整数引用。
clientsTable:
clientID | clientName | clientNoteID | staffNoteID
notesTable:
noteID | note
我希望能够从notesTable中选择clientNoteID引用的注释和staffNoteID引用的注释。
我没有看到任何方法为左连接添加别名,如:
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
答案 0 :(得分:2)
当您加入表时,唉必须紧跟在表名之后,而不是在连接条件之后。试试这个:
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
答案 1 :(得分: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
答案 2 :(得分: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