2015-08-14 60 views
2
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指定ORDERMYSQL順序按由不工作

如何才能實現繪製最後一條記錄,用GROUP

謝謝!

+1

你不需要通過,'選擇manga_source_id,manga_episode_name,manga_episode_number從manga_chapter順序GROUP BY manga_episode_number DESC LIMIT 1;' –

+0

@JorgeCampos我的,因爲我將有另一行需要組不同的記錄rent manga_source_id,我只是想畫出最高的manga_episode_number的每個manga_source_id的1條記錄 –

+0

你需要這個名字嗎? 'manga_episode_name'? –

回答

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; 
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)