在mysql中获取组中的最大值

时间:2016-02-13 04:08:38

标签: mysql

enter image description here

_我将如何在每个anime_id中获得最高的episode_id

---这是我想要的输出

    |id  |episode_id |Anime_id
    |5   |    2      |  1
    |2   |    5      |  2
    |8   |    1      |  3
    |12  |    4      |  4

2 个答案:

答案 0 :(得分:0)

简单分组查询

select anime_id,max(episode_id) as episode_id from episode group by anime_id 

答案 1 :(得分:0)

您可以根据select t1.id, t1.episode_id, t1.anime_id from( select id, episode_id, anime_id,( case anime_id when @curA then @curRow := @curRow + 1 else @curRow := 1 and @curA := anime_id end ) as rn from episode t, (select @curRow := 0, @curA := '') r order by anime_id, episode_id desc )t1 where t1.rn = 1; 的分组给出一个人数,并按episode_id按降序排序。

<强>查询

account_vip

SQL Fiddle demo