2013-03-06 89 views

回答

0

我相信下面的(较短)的声明应为MySQL工作:

SELECT COUNT(val='X'), COUNT(val='Y'), COUNT(val='Z') FROM ... 
2

像这样的事情?

select 
    sum(case when val = 'x' then 1 else 0 end) as countX 
    ,sum(case when val = 'y' then 1 else 0 end) as countY 
    ,sum(case when val = 'z' then 1 else 0 end) as countZ 
from table 
3
SELECT yourcolumn, COUNT(*) AS cnt 
FROM yourtable 
WHERE yourcolumn IN ('X', 'Y', 'Z') 
GROUP BY yourcolumn 
+0

这在我看来是最好的选择(并在Yaugen之前回答)。如果列索引,这将利用索引寻求。我相当肯定执行总和+“情况下,当”组合都将导致表/索引扫描这将是在大量的数据要慢得多 – 2013-03-06 14:18:16

0

我建议如下:

SELECT YourColumn, COUNT(<Any Column of your table>) 
FROM YourTable 
GROUP BY YourColumn 
相关问题