记一下在项目中出现的一个小问题,由于之前没用过case when函数,所以还是费了不少时间
问题:
在一个项目中,有个需求是需要分别统计周一到周日出现的次数,表如下:
解决方案:使用case when 函数
SELECT count(case when(day="Mon") then 1 else null end)as Mon ,
count(case when(day="Tue") then 1 else null end)as Tue,
count(case when(day="Wed") then 1 else null end)as Wed,
count(case when(day="Thu") then 1 else null end)as Thu,
count(case when(day="Fri") then 1 else null end)as Fri,
count(case when(day="Sat") then 1 else null end)as Sat,
count(case when(day="Sun") then 1 else null end)as Sun from zhb_dormitory.t_b_visitor
结果: