【问题标题】:Numeric value out of range: 1690 BIGINT UNSIGNED value is out of range in数值超出范围:1690 BIGINT UNSIGNED 值超出范围
【发布时间】:2026-02-04 06:35:01
【问题描述】:

我有一个问题:

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)'中的值超出范围

不一样。

【问题讨论】:

标签: mysql sql numbers integer-overflow


【解决方案1】:

这将起作用:

 set `points` = `points` - cast(2 AS SIGNED)

`updated_at` = '2019-04-17 23:07:11'

【讨论】:

  • 不确定这将如何工作。它仍然会尝试将 -2 插入到 unsigned 数据类型的列中。 dbfiddle.uk/…
【解决方案2】:

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

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

或者简单地说:

SET points = points - LEAST(points, 2)

【讨论】:

  • 或:UPDATE shops SET points = points - 2, updated_at = '2019-04-17 23:07:11' WHERE id = 4 AND points >= 2;
最近更新 更多