求救:mysql如何用一条sql查询出index等于1和不等1的数量

test表如下

select (select count(index) where index =1),(select count(index) where index !=1) from test

还可以用case when语句

 select   
sum(case   when index=1 then 1 else 0 end) as index_one ,  
sum(case   when index=1 then 0 else 1 end) as index_not_one  
from  test

感觉是不是 可以考虑直接查全表, 统计 index 不为空的 总记录数; 等于 1 和不等于 1 那几乎包含了 所有。

select sum(case when index =1 then 1 else 0 end) as 等于,
sum(case when index<>1 then 1 else 0 end) as 不等于
from 表

凑热闹

  select   
          sum(if(index=1,1,0)) as count_one ,  
          sum(if(index=1,0,1)) as count_not_one  
  from  test