2012-08-01 128 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')