MYSQL SELECT 2计入相同的字段

时间:2012-07-11 15:22:46

标签: mysql

想象一下这个db结构:

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`correct` tinyint(1) NOT NULL DEFAULT '0'

我想在一个查询中获得correct = 1所有条目的计数correct = 0的所有条目的计数。

我该怎么做?

3 个答案:

答案 0 :(得分:4)

使用GROUP BY可以解决问题:

SELECT correct, COUNT(*) FROM table GROUP BY correct;

答案 1 :(得分:1)

select count(case when correct = 0 then 1 end) as ZeroCount,
    count(case when correct = 1 then 1 end) as OneCount
from MyTable

答案 2 :(得分:0)

如果你想要一行中的计数:

SELECT SUM(correct=0) as number_of_zeros,SUM(correct=1) as number_of_ones
FROM table;

如果您想要多行:

SELECT correct,COUNT(*)
FROM table
GROUP BY correct;