shellbot97
shellbot97

Reputation: 328

how to apply join with two columns on a single table

I have 2 tables as following,

USER                         
id     name                
================            
1      user1                 
2      user2
3      user3                 

DETAILS 
value    created_by   updated_by 
===================================
abc         1             2 
def         1             3   

is it possible to apply join in such a way that my resulting structure looks like,

QUERY RESULT

DETAILS.value    USER.created_by   USER.updated_by 
========================================================
abc                     user1             user2 
def                     user1             user3 

Upvotes: 0

Views: 44

Answers (1)

Ankit Bajpai
Ankit Bajpai

Reputation: 13509

1 of the methods is to join table twice -

SELECT D.value `DETAILS.value`, U.name `USER.created_by`, U2.name `USER.updated_by`
FROM DETAILS D
JOIN USER U ON D.created_by = U.id
JOIN USER U2 ON D.updated_by = U2.id 

Upvotes: 2

Related Questions