这个错误我的想法我有这张桌子
CREATE TABLE `t_slot` (
`id` int unsigned NOT NULL,
`uid` int unsigned NOT NULL,
`code` char(9) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`pType` tinyint unsigned NOT NULL,
`pos` tinyint unsigned DEFAULT NULL,
`pid` int unsigned DEFAULT NULL,
`lChild` int unsigned DEFAULT NULL,
`rChild` int unsigned DEFAULT NULL,
`encodeTime` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
然后我试着做这样的查询
SELECT * FROM t_slot WHERE code = 5;
令人惊讶的是,它匹配 1 行的值为
mysql> select * from t_slot where code = 5;
+----+-----+-----------+-------+------+------+--------+--------+---------------------+
| id | uid | code | pType | pos | pid | lChild | rChild | encodeTime |
+----+-----+-----------+-------+------+------+--------+--------+---------------------+
| 2 | 4 | 5FPSLB-BP | 40 | 0 | 1 | 3 | 5 | 2022-03-19 17:39:03 |
+----+-----+-----------+-------+------+------+--------+--------+---------------------+
1 row in set, 50 warnings (0.00 sec)
并收到此警告
mysql> show warnings;
+---------+------+-----------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'Y8VHM9-BP' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '5FPSLB-BP' |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'HBV29S-BP' |
... etc
我真的没想到 5 会与5FPSLB-BP
.
5怎么等于5FPSLB-BP
。
“截断不正确的 DOUBLE 值”——向我们展示导致这些错误的命令。这听起来像
LOAD DATA
没有OPTIONALLY QUOTED
或什么的。WHERE code = 5
-- 将CHAR
-type 列与数字文字 (5
) 进行比较时,字符串将转换为数字。是的,这很令人惊讶,但这就是 MySQL 中发生的事情。由于
code
是“5FPSLB-BP”,您可能应该引用您要与之比较的字符串:WHERE code = "5"
. 在这种情况下,测试将正确失败。