mysql - 数值超出范围: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)'
是不同的。
解决方案
您不能将负值存储在无符号整数中。更安全的解决方案是在执行减法之前检查操作数:
SET points = CASE WHEN points >= 2 THEN points - 2 ELSE 0 END
或者简单地说:
SET points = points - LEAST(points, 2)
推荐阅读
- javascript - JSDoc:如何声明内联参数
- java - 有 tcp 连接或在以太网而不是 wifi 上强制发送 Http 请求
- sql - 我可以在表中有 2 个外键指向同一列吗?
- unreal-engine4 - 错误:无法加载游戏模块
- mysql - 如何将 MySQL MEMORY 存储引擎与 Knex 一起使用?
- javascript - 如何定义从 json 到 js 的 var?
- r - 使用 R 在组中创建具有条件的新数据框
- c# - Css 类不适用于中继器中的最后一个元素
- python - 您可以将 sha256 两次应用于单个字符串吗?
- laravel - Laravel Nova - 前端的输出形式?