在GROUP之后的MYSQL排序不工作

时间:2015-08-14 01:39:49

标签: mysql

select manga_source_id, manga_episode_name, manga_episode_number
from manga_chapter

+-----------------+--------------------------------------+----------------------+
| manga_source_id | manga_episode_name                   | manga_episode_number |
+-----------------+--------------------------------------+----------------------+
|               5 | A Method to Make the World Gentle 1  |                    1 |
|               5 | A Method to Make the World Gentle 2  |                    2 |
|               5 | A Method to Make the World Gentle 3  |                    3 |
|               5 | A Method to Make the World Gentle 5  |                    5 |
|               5 | A Method to Make the World Gentle 6  |                    6 |
|               5 | A Method to Make the World Gentle 7  |                    7 |
|               5 | A Method to Make the World Gentle 8  |                    8 |
|               5 | A Method to Make the World Gentle 9  |                    9 |
|               5 | A Method to Make the World Gentle 10 |                   10 |
|               5 | A Method to Make the World Gentle 11 |                   11 |
|               5 | A Method to Make the World Gentle 12 |                   12 |
|               5 | A Method to Make the World Gentle 13 |                   13 |
|               5 | A Method to Make the World Gentle 14 |                   14 |
|               5 | A Method to Make the World Gentle 15 |                   15 |
|               5 | A Method to Make the World Gentle 16 |                   16 |
|               5 | A Method to Make the World Gentle 17 |                   17 |
|               5 | A Method to Make the World Gentle 18 |                   18 |
|               5 | A Method to Make the World Gentle 19 |                   19 |
|               5 | A Method to Make the World Gentle 20 |                   20 |
|               5 | A Method to Make the World Gentle 21 |                   21 |
|               5 | A Method to Make the World Gentle 22 |                   22 |
|               5 | A Method to Make the World Gentle 23 |                   23 |
|               5 | A Method to Make the World Gentle 24 |                   24 |
|               5 | A Method to Make the World Gentle 25 |                   25 |
|               5 | A Method to Make the World Gentle 26 |                   26 |
+-----------------+--------------------------------------+----------------------+
25 rows in set (0.00 sec)

select manga_source_id, manga_episode_name, manga_episode_number
from manga_chapter
GROUP by manga_source_id
ORDER BY manga_episode_number DESC

+-----------------+-------------------------------------+----------------------+
| manga_source_id | manga_episode_name                  | manga_episode_number |
+-----------------+-------------------------------------+----------------------+
|               5 | A Method to Make the World Gentle 1 |                    1 |
+-----------------+-------------------------------------+----------------------+

我试图获得最后一个manga_episode_number 让世界变得温和的方法26 然而它不起作用,即使我通过manga_episode_number DESC指定ORDER

如何使用GROUP by

实现绘制最后一条记录

谢谢!

2 个答案:

答案 0 :(得分:0)

使用子查询获取最大剧集,然后加入以获取其余列:

select c.*
from manga_chapter c join
     (select manga_source_id, max(manga_episode_number) as maxmen
      from manga_chapter
      group by manga_source_id
     ) cc
     on c.manga_source_id = cc.manga_source_id and
        c.manga_episode_number = cc.maxmen;

答案 1 :(得分:0)

由于您需要查询的漫画名称,并且您有其他ID,这是您想要的查询:

SELECT mc.manga_source_id, 
       mc.manga_episode_name, 
       mc.manga_episode_number
  FROM manga_chapter mc INNER JOIN
        ( SELECT manga_source_id, max(manga_episode_number) mx
            FROM manga_chapter
           GROUP BY manga_source_id ) mc_max
                      ON (mc.manga_source_id = mc_max.manga_source_id)
                          AND mc.manga_episode_number = mc_max.mx)