开发者

How to write the mySQL using left-join for this type of problem?

How do I construct the mySQL query using left-join for this problem?

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

I wish to get a table开发者_C百科 like that

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

The trick is to left join and then alias the table as a or b. That way you can make multiple joins to the same table

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜