简体   繁体   中英

Multiple sql joins same table

I have this query:

SELECT b.id, b.date, members.username, b.type, b.amount, FROM b LEFT JOIN members ON b.user_id = members.id WHERE a.something = 1

Now I want to select another members.username , but now I want it to be by the b.other_user_id .

How can I achieve that?

Join the members table with a different alias on the required condition and select the required column from that table.

SELECT b.id, b.date, m1.username, b.type, b.amount, m2.username
FROM b 
LEFT JOIN members m1 ON b.user_id = m1.id AND a.something = 1
LEFT JOIN members m2 ON b.other_user_id = m2.id 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM