gpt4 book ai didi

mysql - 找不到结果时返回 '0'

转载 作者:行者123 更新时间:2023-11-29 00:43:08 31 4
gpt4 key购买 nike

如果没有找到记录,是否可以修改此查询以在成绩列中返回“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;

Score 和 Grade 是故意的,因为我使用单独的脚本对分数应用字母等级。

最佳答案

看看 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;

我还没有测试查询,所以你可能需要安排它......但是接受这个想法......

如果你只想在列返回空结果时得到零,你可以使用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

关于mysql - 找不到结果时返回 '0',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11403402/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com