查看总计(以下格式)

时间:2019-06-18 17:13:09

标签: mysql

使用以下代码:

SELECT c.idBase, t.Estado FROM argentina.Tickets_2018 t
LEFT JOIN argentina.ContenidoBases c 
ON t.ANI = c.ANI 
WHERE t.idCampania = 8 AND t.Fecha BETWEEN "2018-04-04" AND "2018-04-15"

检索到的列和数据如下:

Base  |   Estado   
----------------
139   |  ANSWER
139   |  ANSWER
139   |  NOANSWER
139   |  NOANSWER
139   |  ANSWER

我想要的是查看这样的信息(总计):

Base  |   Estado   |   Total
----------------
139   |  ANSWER    |     3
139   |  NOANSWER  |     2 

我在代码中做错了什么? (我有很多“基础”(例如139、45、2等))

2 个答案:

答案 0 :(得分:1)

SELECT c.idBase, t.Estado, count(*) as total FROM argentina.Tickets_2018 t
LEFT JOIN argentina.ContenidoBases c 
ON t.ANI = c.ANI 
WHERE t.idCampania = 8 AND t.Fecha BETWEEN "2018-04-04" AND "2018-04-15"
Group by c.idBase, t.Estado

group by将允许您进行计数,这将为您提供总计:)

答案 1 :(得分:0)

使用COUNT()GROUP BY,可能会出现以下结果:

SELECT c.idBase, t.Estado, COUNT(c.idBase) AS `Total`
FROM argentina.Tickets_2018 t
LEFT JOIN argentina.ContenidoBases c ON t.ANI = c.ANI 
WHERE t.idCampania = 8 AND t.Fecha BETWEEN "2018-04-04" AND "2018-04-15"
GROUP BY c.idBase, t.Estado

从预期的输出中可以看到,Baseid列的别名。如果是这样,请使用SELECT id AS Base,GROUP BY c.id。如果不是,则您的实际列名称为idBase,请使用上面的查询。