1
我有一个数据集,看起来像这样:如何一组数据中进行排序,并在一个MySQL查询每个组内的最大值的组进行排序
id entry_id the_data
1 100 Z
2 300 B
3 100 C
4 200 F
5 300 Q
6 300 A
7 300 L
8 100 N
9 200 O
结果应只给出3最近记录(通过id)为每个相应的条目(通过entry_id),它们需要按照entry_id分组在一起,按ID排序(按id)和(棘手的部分)分组集合需要按最近的顺序排序记录(按子集中的最高ID),在下一个子集内次高,等等。
所以,理想的结果应该是这样的:
id entry_id the_data
9 200 O
4 200 F
8 100 N
3 100 C
1 100 Z
7 300 L
6 300 A
5 300 Q
这个查询靠拢,但未能每组
select t1.id, t1.entry_id, t1.the_data
from mytable t1
where (
select count(t2.id) from mytable t2
where t2.entry_id = t1.entry_id and t2.id > t1.id
) <= 2
order by t1.entry_id desc, t1.id desc
结果一看内的记录由Max ID的分组排序像这样:
id entry_id the_data
7 300 L
6 300 A
5 300 Q
9 200 O
4 200 F
8 100 N
3 100 C
1 100 Z
如何完成这个查询来给出想要的以上结果?
美丽!这使我的结果集看起来完全符合我的要求。谢谢! – Adam 2010-08-14 15:28:14