Mysql查询返回每个不同字段的事件

Mysql查询返回每个不同字段的事件,mysql,Mysql,我有下表,如何返回每个状态为pass的id的出现次数 结果集如下所示 id num_occurrences 111 1 222 1 333 2 试试这个: select id, count(id) as num_occurrences from table where status="pass" group by id 查询可能是这样的 SELECT COUNT(id) AS num_occurrences FROM tableName WHERE status = 'pass' 但有一

我有下表,如何返回每个状态为pass的id的出现次数

结果集如下所示

id  num_occurrences
111 1
222 1
333 2
试试这个:

 select id, count(id) as num_occurrences from table where status="pass" group by id

查询可能是这样的

SELECT COUNT(id) AS num_occurrences FROM tableName WHERE status = 'pass'
但有一件事。id通常是唯一的,因为你不应该称它为columnname id

如果你能向OP解释为什么这是正确的,你的答案会更好。 SELECT COUNT(id) AS num_occurrences FROM tableName WHERE status = 'pass'