今天我使用一个查询
select 1000/0
预期结果一定是错误或异常,但 mysql 返回 null ,任何人都可以告诉我为什么。它返回此消息
最佳答案
这是well-documented行为:
By default, division by zero produces a result of NULL and no warning. By setting the SQL mode appropriately, division by zero can be restricted.
With the
ERROR_FOR_DIVISION_BY_ZERO
SQL mode enabled, MySQL handles division by zero differently:
If strict mode is not enabled, a warning occurs.
If strict mode is enabled, inserts and updates involving division by zero are prohibited, and an error occurs.
我永远不会依赖这种行为(仅仅因为大多数其他数据库都会生成错误)。使用:
select 100 / nullif(0, 0)
因此您可以显式生成 NULL
值,而不是依赖数据库的设置。
关于mysql - 为什么MySQL返回null 1000/0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329332/