1.sum(if)
select
sum(if(status=1,1,0)) as s1_count,
sum(if(status=2,1,0)) as s2_count
from order;
2.count()
select
count(status=1 or null) as s1_count,
count(status=2 or null) as s2_count
from order;
注意:count是返回匹配条件的行数,只要有值就会算进去,所以这里不符合条件的算作Null
1.sum(if)
select
sum(if(status=1,1,0)) as s1_count,
sum(if(status=2,1,0)) as s2_count
from order;
2.count()
select
count(status=1 or null) as s1_count,
count(status=2 or null) as s2_count
from order;
注意:count是返回匹配条件的行数,只要有值就会算进去,所以这里不符合条件的算作Null