我需要使用分组依据查找表中的所有状态:

SELECT status FROM table GROUP BY status


然后计算发现的结果

SELECT count(id) WHERE status = [STATUS]


像这样的表:

+--------------------+
| ID | STATUS | NAME |
+--------------------+
|0001| BOUGHT | JOHN |
----------------------
|0002| BOUGHT | JOHN |
----------------------
|0003| CANCEL | JOHN |
----------------------
|0004| BOUGHT | JOHN |
----------------------
|0005| CANCEL | JOHN |
---------------------
|0006| BOUGHT | JOHN |
----------------------
|0007| BOUGHT | JOHN |
----------------------
|0008| SAVE   | JOHN |
----------------------
|0009| SAVE   | JOHN |
----------------------
|0010| BOUGHT | JOHN |
----------------------
|0011| SAVE   | JOHN |
----------------------


将产生如下结果:

STATUS | COUNT
---------------
SAVE   | 3
BOUGHT | 6
CANCEL | 2


任何帮助表示赞赏。

最佳答案

使用以下查询:

SELECT status, count(id) as CNT FROM table GROUP BY status

关于mysql - 合并分组并计数mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45057595/

10-16 17:10