>데이터 베이스 >MySQL 튜토리얼 >mysql float double 部类

mysql float double 部类

WBOY
WBOY원래의
2016-06-07 16:27:131173검색

mysql float double 类型 1.float类型 float列类型默认长度查不到结果,必须指定精度, 比如 num float, insert into table (num) values (0.12); select * from table where num=0.12的话,empty set。 num float(9,7), insert into table (num) values (0.1

mysql float double 类型

1.float类型
float列类型默认长度查不到结果,必须指定精度,
比如 num float, insert into table (num) values (0.12); select * from table where num=0.12的话,empty set。
num float(9,7), insert into table (num) values (0.12); select * from table where num=0.12的话会查到这条记录。

mysql> create table tt
-> (
-> num float(9,3)
-> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into tt(num)values(1234567.8);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
注:超出字段范围,无法插入

mysql> insert into tt(num)values(123456.8);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
+------------+
1 row in set (0.00 sec)
注:小数位数不够,自动补齐,但是存在一个问题就是如上的近似值。

mysql> insert into tt(num)values(123456.867);
Query OK, 1 row affected (0.04 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
+------------+
3 rows in set (0.00 sec)

mysql> select * from tt where num=123456.867;
+------------+
| num |
+------------+
| 123456.867 |
+------------+
1 row in set (0.00 sec)

mysql> insert into tt(num)values(2.8);
Query OK, 1 row affected (0.04 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
| 2.800 |
+------------+
4 rows in set (0.00 sec)

mysql> select * from tt where num=2.8;
+-------+
| num |
+-------+
| 2.800 |
+-------+
1 row in set (0.00 sec)

mysql> insert into tt(num)values(2.888888);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
| 2.800 |
| 2.889 |
+------------+
5 rows in set (0.00 sec)
注:小数位数超了,自动取近似值。

--------------------------------------------------------------------------------------

2.double类型

mysql> create table tt(
-> num double(9,3)
-> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tt(num) values(234563.9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
+------------+
1 row in set (0.00 sec)

mysql> insert into tt(num) values(2345623.2);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into tt(num) values(234563.2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
| 234563.200 |
+------------+
2 rows in set (0.00 sec)

mysql> insert into tt(num) values(2.8);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
| 234563.200 |
| 2.800 |
+------------+
3 rows in set (0.00 sec)

FLOAT(M,D)或REAL(M,D)或DOUBLE PRECISION(M,D)。这里,“(M,D)”表示该值一共显示M位整数,其中D位位于小数点后面。
例如,定义为FLOAT(7,4)的一个列可以显示为-999.9999。MySQL保存值时进行四舍五入,因此如果在FLOAT(7,4)列内插入999.00009,近似结果是999.0001。

1楼yc73695昨天 15:58
顶一个啦
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.