select distinct A.name from table A order by (select sum(num) as num from table where name=A.name) desc
一个表table,name字段有重复,num字段是数字。。显示不重复的name,然后相同的name的num加总数量排序。。
上面这条SQL结果是正确的,就是非常慢。一万多条记录就很慢了,原因是在排序那里。。
请问怎么修改才能又快又达到这样的排序功能?
select name, sum(num) as xxx from table group by name order by xxx desc
统计是不会快的,增加一个冗余的统计表才是正道.
select name, sum(num) as xxx from table group by name order by xxx desc