首页 > 解决方案 > 加入两个表,按列分组并获取所有列

问题描述

**更新:这是架构: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”列,我不知道如何进行另一个加入来添加它们?什么是正确的语法?

谢谢!

标签: mysqlsql

解决方案


我相信您可以通过将其添加到您的子查询中来实现这一点。

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

推荐阅读