如何使用left-join为此问题构造mySQL查询?
Feed Table
-id
-lastpost_id (The id of the user from USER TABLE who last posted)
-firstpost_id (The id of the user from USER TABLE who first posted)
User Table
-id
-name
Run Code Online (Sandbox Code Playgroud)
我想得到一张这样的桌子
Result
-id
-lastpost_id
-lastpost_name
-firstpost_id
-firstpost_name
Run Code Online (Sandbox Code Playgroud)
select *, a.name as lastpost_name, b.name as firstpost_name from Feed f
left join user a on f.lastpost_id=a.id
left join user b on f.firstpost_id=b.id
Run Code Online (Sandbox Code Playgroud)
诀窍是保持连接,然后将表别名为a或b.这样,您可以对同一个表进行多个连接