我有一个名为LOG的表,在此表中我有一个名为Level的列,并且有3个可能的值:' INFO','警告','错误&#39 ;
我想计算每个值的计数,所以我的查询是
SELECT Level, COUNT(*) AS Count FROM LOG GROUP BY Level
我得到的结果是' ERROR':X,' INFO':Y,' WARN':Z。
我有第二栏名为' Logger'这可能会占用很多价值,例如'上传数据'。现在我想仅为Logger ='上传数据'
添加WHERE条件SELECT Level, COUNT(*) AS Count FROM LOG WHERE Logger='Upload Data' GROUP BY Level
现在我得到的结果是' ERROR':X,' INFO':Y。
' WARN'丢失
这是我的问题。
如何修改此查询以返回此结构 '错误':X,'信息':Y,'警告': 0 。
如果没有数据?
答案 0 :(得分:1)
您可以使用UNION ALL
:
SELECT Level, SUM(`Count`) AS `Count`
FROM (
SELECT Level, COUNT(*) AS `Count` FROM LOG WHERE Logger='Upload Data' GROUP BY Level
UNION ALL
SELECT Level, 0 AS `Count` FROM LOG GROUP BY Level
) t
GROUP BY Level
答案 1 :(得分:0)
试试这个 -
SELECT
Level,
COUNT(IF(Logger='Upload Data', Logger, NULL)) AS COUNT
FROM LOG
GROUP BY Level