多个sql连接同一个表

时间:2016-08-01 19:38:07

标签: mysql sql join

我有这个问题:

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

现在我想选择另一个members.username,但现在我希望它来自b.other_user_id

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

使用所需条件的其他别名加入members表,并从该表中选择所需的列。

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