获取MySQL中具有特定值的列的计数

时间:2013-10-16 05:47:02

标签: mysql count

我需要获取特定id的列数,但仅当列值为“Yes”时才需要。我认为以下是错误的。 mid(PRIMARY KEY)

SELECT COUNT(mt.currentstate = 'Yes') AS currentstate FROM mytable mt WHERE mid = 100 GROUP BY mid;

对于COUNT值,我只需要mt.currentstate ='Yes'列,但对于ResultSet,我需要获取mt.currentstate ='Yes'或'No'的所有值

3 个答案:

答案 0 :(得分:3)

你可以尝试以下查询,如果我没有注意它会工作。

SELECT COUNT(mt.mid) AS counter FROM mytable mt WHERE mid = 100 AND mt.currentstate = "YES" GROUP BY mid;

谢谢!

答案 1 :(得分:1)

试试这个:

SELECT COUNT(*) AS currentstate FROM mytable WHERE mid = 100 AND currentstate='Yes';

编辑:“现在如果我在WHERE子句中放入mt.currentstate ='Yes',它将过滤mt.currentstate ='Yes'的所有行,但我需要行mt.currentstate ='中的其他列值不,'我要做什么?'

SELECT currentstate, COUNT(*) AS c FROM mytable WHERE mid = 100 GROUP BY currentstate;

答案 2 :(得分:0)

SELECT COUNT(mt.mid) AS currentstate FROM mytable mt
WHERE mid = 100 and mt.currentstate = 'Yes' GROUP BY mid;