我想计算有多少行给定值(1)并计算有多少行有值(0)
id XXX 1 <== value 0 or 1
id YYY 1
id ZZZ 0
所以结果将是
ones | zeros
2 | 0
事先提前
答案 0 :(得分:2)
这样的事情应该有效:
select sum(case when value = 1 then 1 else 0 end) ones
, sum(case when value = 0 then 1 else 0 end) zeros
答案 1 :(得分:1)
试试这个
select sum(if(value=0,1,0)) as zeros,
sum(if(value=1,1,0)) as ones
from mytable