group by range in mysql

后端 未结 8 1464
一向
一向 2020-12-05 14:31
Table:   
new_table                                                    
user_number  | diff                  
     2       |  0                      
     1       |          


        
相关标签:
8条回答
  • 2020-12-05 15:34

    If you have regular ranges, a quicker solution would be to group with the help of div function.

    For instance:

    select diff div 20 as range, sum(user_number)
    from new_table
    group by diff div 20;
    

    Ranges are represented as single digits in that case and you have to know what they mean: 0 = 0-19, 1 = 20-39, 2 = 40-59,...

    If you need different ranges use different divider or maybe subtract some number from diff. For instance "(diff - 1) div 10" gives you ranges 1-10, 11-20, 21-30,...

    0 讨论(0)
  • 2020-12-05 15:37

    You might want to check Are square brackets valid in an SQL query?

    I suspect that '[' and ']' are used in Microsoft's SQL but not mysql.

    0 讨论(0)
提交回复
热议问题