pankaj pankaj - 6 months ago 19
SQL Question

SQL with inner join and subquery

I have following query:

SELECT * FROM thread where sender = 135 or receiver = 135 order by updatedttm desc


The above query returns correct record which has a senderid and receiverid. The senderid and receiverid are both present in user table. I want to get names of both senderid and receiverid by joining user table to above query. Can someone please help me with this?

Regards

Pankaj

vkp vkp
Answer

You can join the user table twice, once with the sender id and the receiver id and get the name.

SELECT t.*,usend.user_name sender_name, urec.user_name receiver_name
FROM thread t
JOIN users usend on usend.user_id = t.sender
JOIN users urec on urec.user_id = t.receiver
where sender = 135 or receiver = 135
order by updatedttm desc