【发布时间】:2020-03-29 02:44:35
【问题描述】:
下面是我的数据库结构
User
id name
1 John
2 Doe
3 Smitt
Post
id user_id text
1 1 Hello
2 1 World
3 2 How are you?
4 3 Whatsup!
5 3 High five!
Comment
id post_id text created_at
1 1 Comment on Hello 2019-12-01
2 1 Another comment on Hello 2019-12-02
3 2 Comment on World 2019-12-03
4 1 Latest comment on Hello 2019-12-04
5 1 Some comment 2019-12-05
6 5 Five highs! 2019-12-06
7 4 Same old, same old! 2019-12-07
如何根据 MySQL 中的MAX(created_at) 获取只有一个Comment 记录的用户列表,如下所示?
Result
id name comment_text created_at
1 John Some comment 2019-12-05
2 Doe NULL NULL
3 Smitt Same old, same old! 2019-12-07
条件:由于另一个用例,流程必须从User表转到Comment表,反之则不行!
【问题讨论】:
标签: mysql left-join greatest-n-per-group