MySQL将数据按某一字段的范围分组显示,SQL语句怎么写?

员工表(emp)按工资(sal)分组(3000以下,3000至5000,5000至10000和10000以上),然后统计人数。SQL语句该怎么写?

select elt(interval(sal,0,3000,5000,10000,10000),"1","2","3","4","5") as sort ,count(*) 
from emp group by sort;

select
count(1),
(case when t.sal when t.sal BETWEEN 3000 and 5000 then "3000~5000"
when t.sal BETWEEN 5000 and 10000 then "5000~10000"
when t.sal > 10000 then "大于10000" end) as sal
from emp t
group by (case when t.sal when t.sal BETWEEN 3000 and 5000 then "3000~5000"
when t.sal BETWEEN 5000 and 10000 then "5000~10000"
when t.sal > 10000 then "大于10000" end);

参考自:MySQL如何优化GROUP BY http://www.data.5helpyou.com/article237.html
MySQL控制流程函数 http://www.data.5helpyou.com/article327.html

http://blog.csdn.net/l1t/article/details/2883496