mysql - 加入两个表,按列分组并获取所有列
问题描述
**更新:这是架构:http ://sqlfiddle.com/#!9/fd6447
我有 2 张桌子:
帖子:
-------------------------------------------------------
| post_id | post_creator_id | post_title |
-------------------------------------------------------
| 1 | 100 | Hello All |
-------------------------------------------------------
| 2 | 14 | Good morning |
-------------------------------------------------------
| 3 | 213 | Lovely Day |
-------------------------------------------------------
| 4 | 55 | Nice Title! |
-------------------------------------------------------
注释:
------------------------------------------------------------------------------
| comment_id | post_id | commenter_id | comment_text | date |
------------------------------------------------------------------------------
| 8 | 1 | 98 | Hello world | 2018-04-27 12:02:22 |
------------------------------------------------------------------------------
| 9 | 4 | 123 | Hi all | 2018-04-27 13:11:11 |
------------------------------------------------------------------------------
| 10 | 4 | 77 | Looking good | 2018-04-27 13:20:17 |
------------------------------------------------------------------------------
| 11 | 1 | 101 | Great idea | 2018-04-27 14:45:15 |
------------------------------------------------------------------------------
在最终结果中,我希望获得每个帖子的最后评论(comment_text),以及关于帖子的一些信息(post_title、post_creator_id)。以及来自评论的日期(不是来自帖子 - 这样我就可以按最后评论日期对其进行排序)所以在上面的例子中,结果应该是
-----------------------------------------------------------------------------------------------------------------------
| comment_id | post_id | commenter_id| comment_text | post_creator_id | post_title | date |
-----------------------------------------------------------------------------------------------------------------------
| 10 | 4 | 77 | Looking good | 55 | Nice Title! | 2018-04-27 13:20:17 |
-----------------------------------------------------------------------------------------------------------------------
| 11 | 1 | 101 | Great idea | 100 | Hello All | 2018-04-27 14:45:15 |
-----------------------------------------------------------------------------------------------------------------------
所以我能做的是加入带有评论的帖子,但我不知道如何添加附加信息(post_title 和 post_creator_id)。
这是我的查询:
select a.*
from comments a
join (
select post_id, max(date_entered) as date_entered
from comments
group by (post_id)
) b on a.post_id = b.post_id and a.date_entered = b.date_entered
这使:
comment_id post_id commenter_id comment_text date_entered
----------- ----------- ----------- -------------------- -----------------------
10 4 77 Looking good 2018-04-27 13:20:17.000
11 1 101 Great idea 2018-04-27 14:45:15.000
但是我缺少“post_title”和“post_creator_id”列,我不知道如何进行另一个加入来添加它们?什么是正确的语法?
谢谢!
解决方案
我相信您可以通过将其添加到您的子查询中来实现这一点。
select a.*
from comments a
join (
select post_id, max(date_entered) as date_entered, post_title, post_creator_id
from comments
group by (post_id)
) b on a.post_id = b.post_id and a.date_entered = b.date_entered
推荐阅读
- testng - 如何在自动化的单独文件夹中生成 Testng 结果?
- google-cloud-platform - PubSubToText 数据流模板的自定义文件名
- javascript - 如何在数据表 json 中使用标题数据?
- python - 在一个矩阵中使用 np.vstack 堆叠列表项
- c++ - MacOS 中 Kafka C++ 的“x86_64 的未定义符号”链接错误
- javascript - 使用 axios.get 作为参数发送数组是未定义的
- c# - ListBox 中的表单不使用 ToString() 来显示列表项
- c++ - `reinterpret_cast` 实际上对任何事情都有好处吗?
- python - 我想裁剪一个深度框架并获得一个裁剪的深度框架,以便我可以在裁剪的深度框架上应用一些方法
- android - 根检查将某些 android 非设备识别为已根 - Dotfuscator