mysql的count ,case when, ifnull 综合语句来实现分段统计

1.背景要求:统计消费金额大于1000元,800到100元,500到800元,500元以下的人数。

select 

count(case when ifnull(total_money,0)>=1000 then a.user_id end) as '大于100',

count(case when ifnull(total_money,0)>=800 and ifnull(total_money,0)<1000 then a.user_id end) as '大于100',

count(case when ifnull(total_money,0)>=500 and total_money<=800 then a.user_id end) as '大于800小于1000',

count(case when ifnull(total_money,0)>=1000 then a.user_id end) as '大于500 小于800‘,

count(case when ifnull(total_money,0)<=500 then a.user_id end) as '小于500‘

from a

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章