我想指望有多少次confirmed_at没有空给一个promotion_id或者是1483或1887年。MySQL的:如果嵌套总结
select
IF(promotion_id IN(1483,1887), sum(IF(confirmed_at IS NOT NULL,1,0)),0) as all_us_vs_voda,
status_inv
from blabla
group by status_inv
例子:
promotion_id status_inv confirmed_at
1 'before' 2016-05
1483 'before' NULL
1483 'after' 2016-05
1483 'after' 2016-07
1887 'before' 2016-08
1887 'before' 2017-09
1887 'after' 2017-09
因此,其结果必然成为:
status_inv not_nul_specific_promotions
before 2
after 3
PS。我想在select语句中做到这一点。我不想添加“哪里”。
事实上,我能够这样做:
sum(IF(promotion_id IN(1483,1887), IF(confirmed_at IS NOT NULL,1,0),0)) as all_us_vs_voda,
我编辑了我的问题。 – Tonja
我已经添加了另一个版本的答案,但我不明白你为什么不能使用'WHERE'? – Barmar