2012-08-01 138 views
0

對於統計數據的應用領域,具有表結構,是這樣的:Subcount的作爲MySQL查詢

unique_id | browser_family | os | date_time | js_enabled | flash_enabled | non_binary_field 

1   firefox   w7 ...   1   0    yes 
2   chrome   w7 ...   1   1    no 
3   ie9    wx ...   0   0    yes 

所以,我想執行與任何領域的where子句的查詢,並將它給對於那些標準(例如`os` ='w7'和日期(`date_time`)= '01-08-2012'),我計數爲js_enabled = 1,flash_enabled = 0,non_binary_field ='yes'。

其結果將是:

count(js_enabled=1) | count(flash_enabled=1) | count(non_binary_field='yes') 
2      1      1 

這是可能在單個查詢?

謝謝!

回答

3
select sum(js_enabled=1), 
     sum(flash_enabled=1), 
     sum(non_binary_field='yes') 
from your_table 
where `os` = 'w7' 
and date(`date_time`) = '2012-08-01' 
+0

聽起來很簡單!讓我試試看。 – tweak2 2012-08-01 21:34:36

+0

不認爲它會起作用。 sum將column_name作爲參數。 js_enabled不是源表 – 2012-08-01 21:35:11

+0

中的列名,這將工作:)我用了很多。將一個布爾操作賦值給'sum'將會增加'true'個案。 – 2012-08-01 21:36:47

0

每個字段可以由單獨的子查詢填寫:

select 
(select count(js_enabled) from yourtable where js_enabled=1), 
(select count(flash_enabled) from yourtable where flash_enabled=1), 
(select count(non_binary_field) from yourtable where non_binary_field='yes')