如果没有找到记录,有没有办法修改此查询以在Grade列中返回'0'?现在什么都没发现,我得到一个空行。
SELECT COALESCE(Score, 0) AS Score, COALESCE(Grade,0) AS Grade, LAST_UPDATE, '' as dummy
FROM (
SELECT (CASE ScanName WHEN '%=APP%' THEN '= Network System' END) AS System,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Score,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Grade, LAST_UPDATE
FROM missing_internal
WHERE ScanName like '%APP%'
and LAST_UPDATE >= SUBDATE((SELECT MAX(LAST_UPDATE) from missing_internal), INTERVAL 1 day)
HAVING System IS NOT NULL
LIMIT 1
UNION ALL
SELECT NULL AS Score, NULL AS Grade, 0 AS LAST_UPDATE, '' as dummy
ORDER BY LAST_UPDATE DESC
) as T2
LIMIT 1;
分数和成绩与目的相同,因为我使用单独的脚本将分数应用于分数。
答案 0 :(得分:1)
查看COALESCE功能......
如果我很好理解你的问题,下面的查询应该完全按照你想要的那样(当现在返回行时得到零):
SELECT COALESCE(Score, 0) AS Score, COALESCE(Grade,0) AS Grade, LAST_UPDATE
FROM (
SELECT (CASE ScanName WHEN 'APP' THEN 'Network System' END) AS System,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Score,
(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity )) AS Grade, LAST_UPDATE
FROM missing_internal
WHERE ScanName like '%APP%'
and LAST_UPDATE >= SUBDATE((SELECT MAX(LAST_UPDATE) from missing_internal), INTERVAL 1 day)
HAVING System IS NOT NULL
LIMIT 1
UNION ALL
SELECT NULL AS Score, NULL AS Grade, 0 AS LAST_UPDATE
ORDER BY LAST_UPDATE DESC
) AS T2
LIMIT 1;
我还没有测试过这个查询,所以你可能需要安排它...但是想一想......
如果只想在列返回null结果时得到零,则可以使用与上例相同的方式使用COALESCE:
COALESCE(sum(CASE severity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN 3 THEN 6 WHEN 4 THEN 3
END)/COUNT(severity ), 0) AS Grade
答案 1 :(得分:0)
将ELSE
添加到CASE
(sum(CASE severity
WHEN 1 THEN 10
WHEN 2 THEN 9
WHEN 3 THEN 6
WHEN 4 THEN 3
ELSE 0
END)/COUNT(severity )) AS Grade
或使用IFNULL()
IFNULL((sum(CASE severity
WHEN 1 THEN 10
WHEN 2 THEN 9
WHEN 3 THEN 6
WHEN 4 THEN 3
ELSE 0
END)/COUNT(severity )), 0) AS Grade