2015-09-24 213 views
1

我喜歡這個 -MySQL查詢選擇多個計數列

Table

我想運行一個查詢,以便我能有一個像它 -

enter image description here

一個輸出表

我不清楚如何去做。 所以,我所做的是 -

SELECT 
     Count(* where status="Active") as count_active 
     Count(* where status="Inctive") as count_inactive 
     Count(* where status="Biase") as count_biase 
    FROM `subscribers` 

而得到錯誤,任何人都可以請幫助?

回答

3

可以通過Case表達式實現此目的。

查詢

select 
count(case when status = 'Active' then 1 else null end) as Active_Count, 
count(case when status = 'Inactive' then 1 else null end) as Inctive_Count, 
count(case when status = 'Biase' then 1 else null end) as Biase_Count 
from tbl_name; 

SQL Fiddle

+0

Case表達式,而不是case語句... – jarlh

0

這是把它放在一個寄存器方式:

SELECT 
(SELECT count(id) FROM new_table where status = 'Active') as Active_Count, 
(SELECT count(id) FROM new_table where status = 'Inactive') as Inctive_Count, 
(SELECT count(id) FROM new_table where status = 'Biase') as Biase_Count;