2

我的选择:

   SELECT DISTINCT

   (SELECT COUNT(*) 
   FROM t_table 
   WHERE year(date) BETWEEN '2013' AND '2013' AND month(date) BETWEEN '1' AND '12' AND date!=cad_em) as total1,

   (SELECT COUNT(*)
   FROM t_table
   WHERE year(date) BETWEEN '2013' AND '2013' AND month(date) BETWEEN '1' AND '12' AND date=cad_em ) as total2

   FROM t_table
   GROUP BY date

返回:

total1 | total2
1        4    

但我想返回日期示例:

date        | total1 | total2
2013-10     7          8
2013-11     1          4
2013-12     2          5

我怎样才能对我的例子做出同样的反应?

谢谢!!

4

2 回答 2

1

您可以使用条件和来简化事情。另外,我不确定您的 where 条件是否有意义,除非这些是参数-

select
date,
sum( if(date = cad_em, 0, 1) ) as total1,
sum( if(date = cad_em, 1, 0) ) as total2
from
t_table
where year(date) = 2013 and month(date) between 1 and 12
group by date
于 2013-11-07T02:07:21.943 回答
0

我想你想要这样的东西:

select max(`date`) as `date`, sum(`date` <> cad_em) as total1, sum(`date` = cad_em) as total2
from t_table
where year(`date`) = 2013;

您看到想要日期的最大值,但这只是一个猜测。

于 2013-11-07T02:35:39.640 回答