计数和SUM 10列mysql

时间:2016-09-30 15:27:56

标签: php count sum

表名:controleconserto

Table Name controleconserto

我需要计算每列中重复“motivo”的次数,并在添加计数和MAX()之后找到每个“motivo”的最高值

喜欢,但在所有专栏中。

SELECT 
    COUNT(motivo1) AS Total, 
    motivo1 AS reason1 
FROM 
    `controleconserto` 
WHERE 
    fornecedor = 1 
GROUP BY 
    motivo1

输出:

Total | Reason
------+-------
  3   | C1
  1   | C12
  4   | C16
  1   | C6
  3   | C7
  2   | C8

查询:

SELECT MAX(total) 
FROM 
    (SELECT COUNT(motivo1) AS total 
     FROM `controleconserto` 
     WHERE fornecedor = 1 
     GROUP BY motivo1) AS final

输出:

MAX(total)
----------
4

你能帮我吗?

0 个答案:

没有答案