我有一张桌子:
CREATE TABLE `test` (
`t` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
表中的数据是:
mysql> select * from test;
+----------------------+
| t |
+----------------------+
| 12511023900355495873 |
| 12511023900355495872 |
| 12511023900355495874 |
+----------------------+
当我从表中select
时这样:
select * from test where t = 12511023900355495873;
结果是:
+----------------------+
| t |
+----------------------+
| 12511023900355495873 |
| 12511023900355495872 |
| 12511023900355495874 |
+----------------------+
当我使用number
选择varchar
列时会发生什么隐式转换?
答案 0 :(得分:0)
手册页https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html在所有其他情况下,参数将作为浮点(实数)进行比较。
所以
drop table if exists t;
CREATE TABLE t (
t varchar(255) DEFAULT NULL,
t1 float
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into t values
( 12511023900355495873,12511023900355495873 ),
( 12511023900355495872,12511023900355495872 ),
( 12511023900355495874,12511023900355495874 );
select t.*,@t2 from t where t = 12511023900355495873;
结果
+----------------------+-----------+------+
| t | t1 | @t2 |
+----------------------+-----------+------+
| 12511023900355495873 | 1.2511e19 | NULL |
| 12511023900355495872 | 1.2511e19 | NULL |
| 12511023900355495874 | 1.2511e19 | NULL |
+----------------------+-----------+------+
3 rows in set (0.00 sec)
请注意,列t1中的所有浮点值都相同。
不用说,你应该避免类型转换。