我想指望有多少次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