MYSQL join tables multiple times
Asked Answered
L

1

6

I have a table, with rows of events, and each one has (amongst lots of other fields) addedbyuser, editedbyuser, deletedbyuser There are INT, and refer back to the users table to a particular user. I am able to join one of the fields (say addedbyuser) without any problems, how do i join the rest and reference them in php?

events table:

eventid addedbyuser editedbyuser deletedbyuser
1       1           2            3

users table:

id username
1  name1
2  name2
3  name3

So basically, I want to display the names of who added, edited and deleted the article, can I do this in one SQL query?

L answered 10/6, 2011 at 3:27 Comment(0)
F
16

Something like this:

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
Footer answered 10/6, 2011 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.