mysql一条sql查询多个不同条件下的count值(count(if())的用法)!!!(条件太多数据量太大时,性能不是太好,大家慎用哈!!!!!!)
案例
查询数据库中不同日期内的不同状态下的count:
sql语句:
select date(create_time) as dateTime,count(IF(status!=2,true,null)) as count1,count(IF(status=2,true,null)) as count2
from sur_answer where survey_id = 526 group by date(create_time) order by create_time
最后查询的结果为:
查询的结果满足任务要求。
count(if())的用法
count(if(status=1,true,null))中count函数返回一个布尔值类型的数值,如果status=1,返回true,会计数;如果status不等于1返回null,不会计数。
希望能帮到大家!