简体   繁体   English

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

[英]Get count of a column with a particular value in MySQL

I need to get the count of a column for a particular id, but only when the column value is "Yes". 我需要获取特定ID的列数,但仅当列值为“ Yes”时才需要。 Following is wrong i guess. 我想以下是错误的。 mid(PRIMARY KEY) 中(PRIMARY KEY)

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

For the COUNT value only i need from the column mt.currentstate = 'Yes', but for the ResultSet I need to get all the values where mt.currentstate = 'Yes' or 'No' 仅对于COUNT值,我需要从mt.currentstate ='Yes'列中获取,但对于ResultSet,我需要获取mt.currentstate ='Yes'或'No'中的所有值

You can try below query, it will work if i'm not worng. 您可以在下面的查询中尝试,如果我不感到厌倦,它将起作用。

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

Thanks! 谢谢!

Try this: 尝试这个:

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

EDIT: "Now If i put mt.currentstate = 'Yes' inside WHERE clause, it will filter all the rows where mt.currentstate = 'Yes', but I need other column values from the rows mt.currentstate = 'No'..What I'm gonna do?" 编辑:“现在,如果我将mt.currentstate ='是'放在WHERE子句中,它将过滤mt.currentstate ='是'的所有行,但是我需要mt.currentstate ='否'行中的其他列值。我该怎么办?”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM