I have a users table that stores admin and normal customers:
ID Name Role
1 John Admin
2 Sara User
3 Pete User
4 Nick User
Another Table that stores requests:
ID Updatedby Createdby DateCreated DateUpdated
1 1 2 10/12/2017 21/01/2018
2 1 1 11/12/2017 22/01/2018
3 1 3 12/12/2017 23/01/2018
4 2 2 13/12/2017 24/01/2018
I am trying to create a view that looks like this:
ID Updatedby Createdby DateCreated DateUpdated
1 John Sara 10/12/2017 21/01/2018
2 John John 11/12/2017 22/01/2018
3 John Pete 12/12/2017 23/01/2018
4 Sara Sara 13/12/2017 24/01/2018
The problem I am facing when creating the query is, I am trying to left join to the user table twice. How do I specify whether the Users.Name is for Updatedby or Createdby? This is what I mean:
SELECT
request.ID,
users.Name, /*name for Createdby */
users.Name, /*name for Updatedby */
requests.DateCreated,
requests.DateUpdated
FROM
requests
LEFT JOIN
users ON requests.Updatedby = users.ID
I can't get my head around the logic with this.