龙空技术网

MySQL踩坑系列之1

IT工程师1024 85

前言:

此刻朋友们对“mysql5156”可能比较关注,兄弟们都想要分析一些“mysql5156”的相关资讯。那么小编也在网络上搜集了一些有关“mysql5156””的相关文章,希望姐妹们能喜欢,小伙伴们快快来了解一下吧!

目录说明定义时注意事项三者精度区别float场景的问题是精读问题,需运算、比较的的场景,我们尽量不要用floatfloat(M,D)待遇不带,区别很大,如果不指定D,还会给我们截断某些位总结

说明

本文主要介绍float、double、Decimal的相关知识,以及大家日常使用中的误区,以避免后续日常工作中出现意想不到的bug.
定义时注意事项
float(M,D) unsigned  中的M代表可以使用的数字位数,D则代表小数点后的小数位数, unsigned 代表不允许使用负数!double(M,D) unsigned 中的M代表可以使用的数字位数,D则代表小数点后的小数位数decimal(M,D),M代表可以使用的数字位数,D则代表小数点后的小数位数其中,M 的范围是165,D 的范围是030,而且D不能大于M。
三者精度区别

类型名称

存储需求

范围

float

4字节

(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)

double

8字节

(-1.797 693 134 862 315 7 E+308,-2.225073858507 2014E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)

decimail(M,D)

具体字节数需要根据实际情况推算

依赖于M和D的值

float场景的问题是精读问题,需运算、比较的的场景,我们尽量不要用float

(root@localhost) [jedmonitor] >create table test_1(id int ,c_float float(24,6) ,c_doubie double(24,6),c_decimal decimal (24,6));Query OK, 0 rows affected (0.00 sec)(root@localhost) [jedmonitor] >insert into  test_1 values(1,61.61,61.61,61.61);Query OK, 1 row affected (0.00 sec)(root@localhost) [jedmonitor] >select * from  test_1;+------+-----------+-----------+-----------+| id   | c_float   | c_doubie  | c_decimal |+------+-----------+-----------+-----------+|    1 | 61.610001 | 61.610000 | 61.610000 |+------+-----------+-----------+-----------+1 row in set (0.00 sec)--运算结果(root@localhost) [jedmonitor] >select c_float*100000000,c_doubie*100000000,c_decimal*100000000 from  test_1;+-------------------+--------------------+---------------------+| c_float*100000000 | c_doubie*100000000 | c_decimal*100000000 |+-------------------+--------------------+---------------------+| 6161000061.035156 |  6161000000.000000 |   6161000000.000000 |+-------------------+--------------------+---------------------+(root@localhost) [jedmonitor] >select * From test_1;+------+-----------+-----------+-----------+| id   | c_float   | c_doubie  | c_decimal |+------+-----------+-----------+-----------+|    1 | 61.610001 | 61.610000 | 61.610000 |+------+-----------+-----------+-----------+1 row in set (0.00 sec)-- 比较(root@localhost) [jedmonitor] >select * from  test_1 where c_float=61.61;Empty set (0.00 sec)(root@localhost) [jedmonitor] >select * from  test_1 where c_doubie=61.61;+------+-----------+-----------+-----------+| id   | c_float   | c_doubie  | c_decimal |+------+-----------+-----------+-----------+|    1 | 61.610001 | 61.610000 | 61.610000 |+------+-----------+-----------+-----------+1 row in set (0.00 sec)(root@localhost) [jedmonitor] >select * from  test_1 where c_decimal=61.61;+------+-----------+-----------+-----------+| id   | c_float   | c_doubie  | c_decimal |+------+-----------+-----------+-----------+|    1 | 61.610001 | 61.610000 | 61.610000 |+------+-----------+-----------+-----------+1 row in set (0.00 sec)
float(M,D)待遇不带,区别很大,如果不指定D,还会给我们截断某些位

(root@localhost) [jedmonitor] >create table test_1(id int ,c_float1 float,c_float2 float(12,6));

Query OK, 0 rows affected (0.00 sec)

(root@localhost) [jedmonitor] >insert into test_1 values (1,61.60001,61.60001);

Query OK, 1 row affected (0.00 sec)

(root@localhost) [jedmonitor] >select * from test_1;

±-----±---------±----------+

| id | c_float1 | c_float2 |

±-----±---------±----------+

| 1 | 61.6 | 61.600010 |

±-----±---------±----------+

1 row in set (0.00 sec)

(root@localhost) [jedmonitor] >insert into test_1 values (1,61.6001,61.6001);

Query OK, 1 row affected (0.00 sec)

(root@localhost) [jedmonitor] >insert into test_1 values (1,61.601,61.601);

Query OK, 1 row affected (0.00 sec)

(root@localhost) [jedmonitor] >insert into test_1 values (1,61.61,61.61);

Query OK, 1 row affected (0.00 sec)

(root@localhost) [jedmonitor] >select * from test_1;

±-----±---------±----------+

| id | c_float1 | c_float2 |

±-----±---------±----------+

| 1 | 61.6 | 61.600010 |

| 1 | 61.6001 | 61.600101 |

| 1 | 61.601 | 61.601002 |

| 1 | 61.61 | 61.610001 |

±-----±---------±----------+

总结

1.涉及运算比较,尽量用c_decimal2.如果用了float、double,需要注意制定(m,n),否则可能会出现意外的结果

标签: #mysql5156