如何从mysql表中选择Range of Ranges?

时间:2012-04-05 12:16:30

标签: mysql select count

我在mySql中有一个表t_points,如下例所示。

Name          Surname          Point
Joe           Arnold           120
Michale       Black            250
Masha         Petrova          300
Natalie       Jackson          120
John          Turo             200
Bona          Meseda           250
Zeyda         Nura             150
Zanura        Bohara           60
Shaheen       Boz              360
Abbas         Murat            160
Keira         Black            230
Tom           Robinson         480
Fred          Balka            490
Semia         Hudovi           90
Sona          Bahari           60

我想编写一个显示点范围计数的查询。点范围是这样的:指向0到100之间,101和200,201和300,301和400。 结果必须如下所示

0_100           101_200          201_300            301_400
3               5                4                  3

我想你明白我想说什么。那么我必须使用哪个查询来得到这个结果呢? 感谢。

5 个答案:

答案 0 :(得分:11)

select
     count(CASE WHEN point BETWEEN 0 AND 100 THEN 1 END) as count0_100,
     count(CASE WHEN point BETWEEN 101 AND 200 THEN 1 END) as count101_200,
     count(CASE WHEN point BETWEEN 201 AND 300 THEN 1 END) as count201_300,
     ...
from
    t_poits

答案 1 :(得分:1)

类似的东西:

select count(*) as count, abs(point/100) as range 
from t_poits
group by abs(point/100)

答案 2 :(得分:1)

您可以对点列进行分组并执行一些数学运算以指定范围。基于此,您可以计算记录数。

像..

SELECT concat( 101 * round( Point /101 ) , '-', 101 * round( Point /101 ) +100 ) AS `range` , count( * ) AS `result`
FROM t_points
GROUP BY 1
ORDER BY Point

我希望它对你有用。

答案 3 :(得分:1)

    设置@range = 500;

select floor(field1/@range)*@range as `from`,(ceil(field1/@range)+if(mod(field1,@range)=0,1,0))*@range as `to`, count(field1)
from table1
group by 1,2
order by 1,2;

答案 4 :(得分:0)

select
    concat(floor(Point/100),'01_',ceil(Point/100),'00') as ind,
    count(*) as cnt
  from t group by ind order by ind asc;