MySQL Group By with top N number of each kind
问题 I have a table like this: Rank Letter 1 A 2 A 3 B 4 A 5 C 6 A 7 C 8 C 9 B 10 C And I need the top 2 of each letter ordered by ascending rank: Rank Letter 1 A 2 A 3 B 5 C 7 C 9 B How would I do it? It's fairly straightforward to get just the top 1 using GROUP BY, but I can't seem to get it working for multiple entries 回答1: select distinct rank, letter from table1 t2 where rank in (select top 2 rank from table1 t2 where t2.letter = t1.letter order by rank) order by letter, rank EDIT: (my first