gpt4 book ai didi

mysql - 为什么MySQL返回null 1000/0

转载 作者:行者123 更新时间:2023-11-29 11:12:23 25 4
gpt4 key购买 nike

今天我使用一个查询 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/

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