11

我在其他帖子上看到了类似问题的解决方案,但我在将其应用于我的具体问题时遇到了问题。

这是我最初的加入:

SELECT service_note_task, comment_id, comment FROM service_note_task LEFT JOIN service_note_task_comments ON service_note_task.service_note_task_id = service_note_task_comments.service_note_task_id;  

结果是:

+-----------------------------+------------+--------------+
| service_note_task           | comment_id | comment      |
+-----------------------------+------------+--------------+
| This is service note task 3 |         25 | Comment      |
| This is service note task 3 |         26 | Comment Blah |
| This is service note task 3 |         36 | aaa          |
| This is service note task 2 |         13 | Awesome comm |
| This is service note task 1 |         12 | Cool Comm    |
+-----------------------------+------------+--------------+

但是对于每个 service_note_task,我真的只需要一行来表示具有最高 comment_id 的评论,如下所示:

+-----------------------------+------------+--------------+
| service_note_task           | comment_id | comment      |
+-----------------------------+------------+--------------+
| This is service note task 3 |         36 | aaa          |
| This is service note task 2 |         13 | Awesome comm |
| This is service note task 1 |         12 | Cool Comm    |
+-----------------------------+------------+--------------+

我想我可以在子选择语句中使用 MAX 来缩小我想要的结果。我如何将其纳入我的陈述以获得这些结果?

4

4 回答 4

12

作为参考,这被称为“groupwise-maximum”

http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html

于 2013-02-08T16:25:30.750 回答
3

因为您没有提到您正在使用的 RDBMS,所以下面的这个查询主要适用于许多 RDBMS(不是全部

SELECT  a.*, b.*   -- select only the columns you want. 
FROM    service_note_task a
        INNER JOIN service_note_task_comments b
            ON a.service_note_task_id = b.service_note_task_id
        INNER JOIN 
        (
            SELECT  service_note_task_id, MAX(commentID) max_ID
            FROM    service_note_task_comments
            GROUP   BY service_note_task_id
        ) c ON  b.service_note_task_id = c.service_note_task_id AND
                b.commentID = c.max_ID

如果您的 RDBMS 支持分析函数,您可以在下面使用它,

SELECT  a.service_note_task, b.comment_id, b.comment 
FROM    service_note_task a
        INNER JOIN 
        (
            SELECT  service_note_task_id, comment_id, comment,
                    ROW_NUMBER() OVER (PARTITION BY service_note_task_id
                                        ORDER BY comment_id DESC) rn
            FROM    service_note_task_comments
            GROUP   BY
        ) c ON  a.service_note_task_id = b.service_note_task_id AND
                b.rn = 1
于 2013-02-08T15:16:24.403 回答
1

尝试:

SELECT service_note_task, comment_id, comment 
FROM service_note_task SNT1 
LEFT JOIN service_note_task_comments ON service_note_task.service_note_task_id = service_note_task_comments.service_note_task_id
WHERE comment_id = (SELECT MAX(comment_id) FROM  service_note_task SNT2 WHERE SNT1.service_note_task = SNT2.service_note_task);
于 2013-02-08T15:27:09.923 回答
0
SELECT service_note_task, comment_id, comment
FROM service_note_task s LEFT JOIN service_note_task_comments sc
  ON s.service_note_task_id = sc.service_note_task_id;
WHERE EXISTS (
              SELECT 1
              FROM service_note_task_comments s2
              WHERE s.service_note_task_id = s2.service_note_task_id
              HAVING MAX(s2.comment_id) = sc.comment_id
              )
于 2013-02-08T15:29:04.393 回答