2011-06-10 115 views
6

我有一個表,與事件的行,而且每一個(其中包括很多其他領域)addedbyusereditedbyuserdeletedbyuserINT,並參考回到users表中特定用戶。 我可以加入其中一個字段(說addedbyuser)沒有任何問題,我如何加入其餘並在PHP中引用它們?MYSQL連接表多次

事件表:

eventid addedbyuser editedbyuser deletedbyuser 
1  1   2   3 

用戶表:

id username 
1 name1 
2 name2 
3 name3 

所以基本上,我想顯示誰添加,編輯和刪除文章的名字,我能做到這一點的一個SQL查詢?

回答

16

事情是這樣的:

select 
    evn.eventid, 
    us1.username as addedbyuser, 
    us2.username as editedbyuser, 
    us3.username as deletedbyuser, 
from events evn 
    join users as us1 on 
     evn.addedbyuser = us1.id 
    join users as us2 on 
     evn.editedbyuser = us2.id 
    join users as us3 on 
     evn.deletedbyuser = us3.id