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

标签 mysql sql

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

相关文章:

mysql - SQL+获取最新日期的行+row_number()+重复的情况

mysql - 从 SELECT 中对 SQL 元组进行编号时获取 NULL 值

php - mysql中的getdate()

PHP/MySQL 最新照片啦

sql - 仅当按顺序排列时才省略重复项

mysql - 为什么使用内连接时会得到不同的结果

MySQL - WHERE 子句 + JOIN

Mysql结构及很多表之间的关系

c# - Entity Framework Core "The entity type ' XXX'需要定义主键。”

php - 如何修复 CI 事件记录中的此查询?