Mysql 如何使用Count+;独特的+;什么时候

Mysql 如何使用Count+;独特的+;什么时候,mysql,count,case,distinct,Mysql,Count,Case,Distinct,我有这个表(表名): 我执行这些查询得到了正确的结果: 问题1: select Date,count(distinct idControls) from tableName where Status=0 group by Date; +------------+-------------------------------------+ | Date | count(distinct idControls) | +------------+-------------

我有这个表(表名):

我执行这些查询得到了正确的结果:

问题1:

select Date,count(distinct idControls) from tableName where Status=0 group by Date;
+------------+-------------------------------------+
| Date       | count(distinct idControls)          |
+------------+-------------------------------------+
| 2014-12-01 |                                   1 |
| 2014-14-02 |                                   2 |
问题2:

select Date,count(distinct idControls) from tableName where Status=1 group by Date;
+------------+-------------------------------------+
| Date       | count(distinct idControls)          |
+------------+-------------------------------------+
| 2014-11-01 |                                   1 |
| 2014-13-02 |                                   1 |
| 2014-16-02 |                                   1 |
但是,我想做的是将两个查询都包含在一个查询中

然后,我试着这样做:

 select Date,sum(distinct(idControls) case when Status='1' then 1  else 0 end) fail ,sum(distinct case when Status='0' then 1 else 0 end) correct from tableName group by Date;
显然,上面的查询不起作用


有什么想法吗?

可能会有一个稍微不同的方法

SELECT `Date`,
       `status`,
       COUNT(DISTINCT `idControls`)
FROM `tableName` 
GROUP BY `Date`, `status`;
更简单的查询,但每个日期有两个结果行,而不是一个

SELECT `Date`,
       `status`,
       COUNT(DISTINCT `idControls`)
FROM `tableName` 
GROUP BY `Date`, `status`;