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

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