首页 > 解决方案 > 数值超出范围:1690 BIGINT UNSIGNED 值超出范围

问题描述

我有一个查询:

update `shops` set
    `points` = `points` - 2,
    `updated_at` = '2019-04-17 23:07:11'
where `id` = 4;

列点具有列类型:BIGINT(20)。

现在在记录中我的值为 62。当我运行上述查询时,我收到此错误:

SQLSTATE [22003]:数值超出范围:1690 BIGINT UNSIGNED 值超出范围在 '(`database`.`shops`.`points` - 2)'

是不同的。

标签: mysqlsqlnumbersinteger-overflow

解决方案


您不能将负值存储在无符号整数中。更安全的解决方案是在执行减法之前检查操作数:

SET points = CASE WHEN points >= 2 THEN points - 2 ELSE 0 END

或者简单地说:

SET points = points - LEAST(points, 2)

推荐阅读