我有这个SQL请求:
SELECT mailbox.*,
user.picture, user.firstname, user.lastname
FROM mailbox
LEFT JOIN user ON user.id = mailbox.sender_id
WHERE mailbox.sender_id = 54 OR mailbox.receiver_id = 54
我需要在接收者ID上再加上一个LEFT JOIN
:
LEFT JOIN user ON user.id = mailbox.receiver_id
从用户那里获取信息,但是我已经通过第一个LEFT JOIN
那又如何从用户表中获取信息?
答案 0 :(得分:2)
我想你想要
SELECT m.*,
us.picture, us.firstname, us.lastname,
ur.picture, ur.firstname, ur.lastname
FROM mailbox m LEFT JOIN
user us
ON us.id = m.sender_id LEFT JOIN
user ur
ON ur.id = m.receiver_id
WHERE 54 IN (m.sender_id, m.receiver_id);
注意:您这里可能不需要LEFT JOIN
。接收方和发送方id列大概包含有效的用户值。
答案 1 :(得分:1)
如果我正确理解了您的问题,那么应该可以:
SELECT mailbox.*, u1.picture, u1.firstname, u1.lastname, u2.picture, u2.user.firstname, u2.lastname
FROM mailbox
LEFT JOIN user u1 ON u1.id = mailbox.sender_id
LEFT JOIN user u2 ON u2.id = mailbox.receiver_id
WHERE mailbox.sender_id = 54 OR mailbox.receiver_id = 54