• 技术文章 >数据库 >mysql教程

    mysql float double 类型_MySQL

    2016-05-31 08:50:05原创429

    1.float类型

    float列类型默认长度查不到结果,必须指定精度,

    比如 numfloat,insert intotable (num) values (0.12); select* from table where num=0.12的话,empty set。

    num float(9,7),insert intotable (num) values (0.12); select* from table where num=0.12的话会查到这条记录。

    mysql> create table tt

    -> (

    -> numfloat(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* fromtt;

    +------------+

    | 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 * fromtt;

    +------------+

    | num|

    +------------+

    | 123456.797 |

    | 123456.797 |

    | 123456.867 |

    +------------+

    3 rows in set (0.00 sec)

    mysql> select* from tt wherenum=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 * fromtt;

    +------------+

    | num|

    +------------+

    | 123456.797 |

    | 123456.797 |

    | 123456.867 |

    |2.800 |

    +------------+

    4 rows in set (0.00 sec)

    mysql> select* from tt wherenum=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* fromtt;

    +------------+

    | num|

    +------------+

    | 123456.797 |

    | 123456.797 |

    | 123456.867 |

    |2.800 |

    |2.889 |

    +------------+

    5 rows in set (0.00 sec)

    注:小数位数超了,自动取近似值。

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

    2.double类型

    mysql> create table tt(

    -> numdouble(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 * fromtt;

    +------------+

    | 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* fromtt;

    +------------+

    | 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。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:
    上一篇:hive简单操作总结和实例(一)_MySQL 下一篇:JDK+JDBC+MySQL实例及注意事项_MySQL
    PHP编程就业班

    相关文章推荐

    • 详细了解MySQL慢日志查询• 怎么查询mysql的最大连接数• 完全掌握Mysql的explain• jquery怎样排除第一个元素• 深入了解MySQL索引结构

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网