如何使用左连接构建 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
我想要一张这样的 table
Result
-id
-lastpost_id
-lastpost_name
-firstpost_id
-firstpost_name
最佳答案
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
技巧是左连接,然后将表别名为 a 或 b。这样您就可以对同一个表进行多个联接
关于mysql - 对于此类问题,如何使用左连接编写 mySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5239475/