Php 在单个表中使用if进行计数,并在两个单独的列中显示结果-mysql

Php 在单个表中使用if进行计数,并在两个单独的列中显示结果-mysql,php,mysql,mysqli,Php,Mysql,Mysqli,我有一个类似于表的表名perms,我需要得到uid=1和pid=1这样的结果,使用0值作为计数器1进行计数,使用1值作为计数器2进行计数 uid pid oid value 1 1 15 0 1 1 17 1 1 1 18 0 我能得到这样简单的计数 SELECT *,count(*) as counter1 from perms where uid = 1 and pid = 1 and value = 0

我有一个类似于表的表名perms,我需要得到uid=1和pid=1这样的结果,使用0值作为计数器1进行计数,使用1值作为计数器2进行计数

uid    pid  oid  value
 1      1    15    0
 1      1    17    1
 1      1    18    0
我能得到这样简单的计数

SELECT *,count(*) as counter1 from perms where uid = 1 and pid = 1 and value = 0

如何在与单独列相同的查询中获取计数器2

select *,
sum(case when `value` = 0 then 1 else 0 end) as counter1,
sum(case when `value` = 1 then 1 else 0 end) as counter2
from perms 
where uid = 1 
and pid = 1;