我需要修复以下查询:
SELECT *
FROM message_treads
LEFT JOIN usernames ON usernames.uid=message_treads.uid_1
LEFT JOIN usernames ON usernames.uid=message_treads.uid_2
我知道我应该使用别名,但我不知道如何在这里做。我知道当一个表只有一个左连接时如何做,但是当有多个表时怎么做呢?
答案 0 :(得分:3)
你可以这样做:
SELECT *
FROM message_treads
LEFT JOIN usersnames ON usernames.uid IN (message_treads.uid_1, message_tread.uid_2)
至于别名,你可以这样做:
SELECT *
FROM message_treads
LEFT JOIN usernames AS user1 ON user1.uid = message_treads.uid_1
LEFT JOIN usernames AS user2 ON user2.uid = message_treads.uid_2
但这可能不是你想要的,因为这将从doubled-up用户名表中返回两组列。上面的第一个版本只返回一组用户名列。
答案 1 :(得分:2)
你是对的,你需要使用这样的别名:
SELECT *
FROM message_treads
LEFT JOIN usernames uid1 ON uid1.uid=message_treads.uid_1
LEFT JOIN usernames uid2 ON uid2.uid=message_treads.uid_2