Skip to content

Division 通过零

错误信息

                division 通过零
              

快速摘要

你的查询包含 division 操作哪里 denominator 是零, 哪个是 mathematically undefined.

为什么发生

  • 列 used as denominator 包含零 值

  • computed denominator evaluates 到零

  • Aggregating 数据哪里数量 or 总和 used as divisor 是零

最小示例

✗ 错误代码
SELECT total / count FROM stats;
-- If count is 0 for any row → division by zero
✓ 修复代码
SELECT total / NULLIF(count, 0) FROM stats;
-- Returns NULL instead of erroring when count is 0

NULLIF(数量, 0) 返回 NULL 当数量是 0, 所以 division yields NULL 相反的 raising 错误.

如何诊断

  • 查找 division 操作 in 你的查询

  • 检查如果 denominator 列 or 表达式能 是零

  • 运行查询到 查找行 哪里 denominator 是零

如何修复

  • Use NULLIF 到 convert 零到 NULL: numerator / NULLIF(denominator, 0)

  • 添加 WHERE clause 到排除行 哪里 denominator 是零

  • Use CASE 表达式到 handle 零情况 specially

如何预防

  • 总是 use NULLIF for divisions 哪里 denominator 可能是 零

  • 验证数据到 prevent 零 denominators

  • 测试查询使用边缘情况包含零 值

相关资源

相关术语

相关课程

← 返回语言