赞
踩
想要的结果如下:
SELECT * FROM `reports` GROUP BY type ORDER BY score DESC;
结果如下:
order by 是在最后执行并不能查询到想要的结果
SELECT
a.*
FROM
( SELECT * FROM `reports` ORDER BY score DESC) AS a
GROUP BY
a.type;
结果如下:
并没有得道正确的结果
然后查询资料看到有博客说需要在子查询加上limit,如下sql:
SELECT
a.*
FROM
( SELECT * FROM `reports` ORDER BY score DESC LIMIT 10) AS a
GROUP BY
a.type;
结果如下:
结果是对了,但是总觉得奇怪,为什么要加 limit ,这不是我想要的
###尝试通过聚合函数查询,sql如下:
SELECT
s.*, max( s.score )
FROM
( SELECT * FROM `reports` GROUP BY type, name ORDER BY score DESC ) s
GROUP BY
s.type
结果如下:
搞定
max( s.score ) 亦可拿掉
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。