例如:现在表中有3个字段:id,score,name,,根据name分组然后查询出name同一个组中的最大值和最小值(同时查询出),用一条sql语句(例如mysql数据库)。有好的方法么?求指点。
解决方案
10
select * from ( select * from (select id,score,name from tb group by name order by score desc ) union all select * from (select id,score,name from tb group by name order by score) ) as t order by t.name
本人整合下吧
3
SELECT name, MAX(score) as maxs, MIN(score) as mins FROM tbl01 GROUP BY name
7
select tb.* from ( select name, max(score) as max_score, min(score) as min_score from tb group name )t inner join tb on tb.name = t.name and (tb.score = t.max_score or tb.score = t.min_score)