且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Mysql 学习】数值类型转换

更新时间:2022-06-30 16:57:50

数值类型转换问题,当从一个字段从double 变为 float 精度不变,而从float 转变为double 时会发生精度的改变,实验如下:
mysql> create table t2 ( id1 float (5,2) default null, id2 double (5,2) default null, id3 decimal (5,2) default null );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t2 values (1.23,1.23,1.23);
Query OK, 1 row affected (0.01 sec)

mysql> alter table t2 modify id2 double;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------+------+
| id1  | id2  | id3  |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+
1 row in set (0.01 sec)

mysql> alter table t2 modify id2 float;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------+------+
| id1  | id2  | id3  |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+
1 row in set (0.00 sec)

mysql> desc t2;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id1   | float(5,2)   | YES  |     | NULL    |       |
| id2   | float        | YES  |     | NULL    |       |
| id3   | decimal(5,2) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table t2 modify id2 double;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+------+------------------+------+
| id1  | id2              | id3  |
+------+------------------+------+
| 1.23 | 1.23000001907349 | 1.23 |
+------+------------------+------+
1 row in set (0.01 sec)

mysql>