MySQL版本:5.5.38-0ubuntu0.14.04.1
表结构:
CREATE TABLE `route_points2` (
`dist` tinyint(4) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
数据:1 行 dist
等于 200
SQL:
SELECT * FROM route_points2 WHERE -1*dist < 1;
错误:
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(<cache>(-(1)) * `db_name`.`route_points2`.`dist`)'
为什么 mysql 将 -1*dist
转换为 BIGINT UNSIGNED
?
可以通过将 dist
转换为 SIGNED
来解决上面的错误:
SELECT * FROM route_points2 WHERE -1*CAST(dist AS SIGNED) < 1;
这很好用。
但是我不明白为什么mysql为-1*dist
选择了BIGINT UNSIGNED
最佳答案
由于您使用的是 UNSIGNED 数字,并且与 SIGNED 数字 (-1) 进行比较,mysql 需要将您的 UNSIGNED 转换为 SIGNED。我猜它选择 BIGINT 是为了能够应对所有可能的 INT。
MySQL 5.5 改变了行为,看这个 http://dev.mysql.com/doc/refman/5.5/en/out-of-range-and-overflow.html
要完成这项工作,请将您的表定义为:
CREATE TABLE `route_points2` (
`dist` tinyint NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
或者如果你需要值 > 127 使用:
CREATE TABLE `route_points2` (
`dist` smallint NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
关于mysql - 错误 1690 (22003) : BIGINT UNSIGNED value is out of range in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787583/