table test 表结构
id, chapterid, widx
60002 1 1
60004 1 1
60005 1 1
...
...
60006 1 5
60002 3 5
60005 3 4
60008 4 5
问题: 如何把每个id的最大chapterid和最大widx列出来.
思路:
- 我先固定一个值
SELECT id, max(chapterid) as mid from pve_war where id in %s group BY id;
60002 3
60004 1
60005 1
60006 1
2.再把固定的表和原表作为联合查询( 条件为tmp.id=pve_war.id AND tmp.mc=pve_war.chapterid)
关键点为 1.要拿上表的max(chapterid)作为最终的chapteridm再找max(widx) 最后group by一下pve_war.id, tmp.mc
SELECT pve_war.id, tmp.mc, max(widx) FROM pve_war, (SELECT id, max(chapterid) as mc FROM pve_war GROUP BY id) AS tmp WHERE tmp.id=pve_war.id AND tmp.mc =pve_war.chapterid group by pve_war.id, tmp.mc;
网友评论