开发者

MySQL group integer column into groups of 100

I have a MySQL table and I am looking to create a stats page on the data.

Id (integer), Pri (integer), blab1, blab2, blab3, ect...

The "pri"开发者_StackOverflow中文版 field has a number between 0-1000. I would like to how the rows are spread across the pri in the table. I am looking for a way to group the "pri" by 100s and count the number of rows in each group.

For example:

Range   | Count
-----------------
  0- 99 |  999
100-199 |   50 
200-299 | 3587 

The easier way would be to run 10 separate queries for each range, (WHERE pri >= 400 AND pri < 500) but I was wondering if there was a easier way?


You can always trunc the pri column to like so:

SELECT 
    truncate(pri/100,0)*100 AS Range_Start,
    count(*)
FROM ...
GROUP BY
    truncate(pri/100,0)*100;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜