I ran into this little problem today; when dividing two whole number the result MySQL gave me was nowhere near as precise as I needed it to be. The &#118alues were truncated which caused me some errors in my application.
Example:
mysql> select 1*0.00001;
+-----------+
| 1*0.00001 |
+-----------+
| 0.00001 |
+-----------+
Ok, that looks fine. Let's do the same thing using division:
mysql> select 1/100000;
+----------+
| 1/100000 |
+----------+
| 0.0000 |
+----------+
Oops! What happened?
The issue is that the maximum precision of the result &#118alue depends on the number of decimal places in the arguments. Since the second version uses two whole numbers, the result uses the default number of decimal places, which is 4. So you really have to take care to make sure you're getting the precision you want out of your math operations!
To MySQL's credit, the behavior is clearly documented (however, I maintain it's still a bit troublesome because how often would you expect the division operator to have a list of instructions and caveats?)
There are at least a couple simple solutions:
mysql> SELECT CAST(1/100000 AS DECIMAL(8,5) );
+---------------------------------+
| CAST(1/100000 AS DECIMAL(8,5) ) |
+---------------------------------+
| 0.00001 |
+---------------------------------+
Or
mysql> SET div_precision_increment=5;
mysql> SELECT 1/100000;
+----------+
| 1/100000 |
+----------+
| 0.00001 |
+----------+

mysql除法精度
作者:不详转载自:网络更新时间:2009-9-3
- MYSQL中删除重复记录的方法
- 如何快速学习常用的几种数据库对象
- 关于sql2000和mysql2000的异同
- 有关SQL Server 2005数据库的同义词
- 如何对MySQL数据库进行安全配置
- MySQL系列教程之MySQL5入门实践
- 关于SQL Server 数据导入的规范详解
- mysql中如何查看某个数据库或表占用
- 如何暂停binlog的记录
- 如何进行oracel 验证过程
- 关于MYSQL集群的备份与恢复
- SQL Server 2005数据库技术的镜像配
- SQL Server数据库技术的超级管理员
- SQL 2005数据库之转到SQL 2000的步
- SQL Server如何自动生成日期加数字
- SQL Server 2000数据库的容量到底有
- 详解数据库生成xml的两个方法
- Oracle数据库程序开发工具的优点
- SQL Server数据库技术—内容替换方
- SQL教程之常用SQL语句总结
- MySQL教程之几个要注意的TIPS
- Linux下忘记mysql密码解决全方案
- ORACLE数据库常用数据字典
- 使用dbca在裸设备上创建数据库
- Sybase数据备份之BCP用法
- 解除SYBASE ANYWHERE的密码
- sybase常用系统表
- 复位MYSQL根用户密码
- 临时表与内存表的区别
- MyISAM 锁实例
- MyISAM 和Innodb 数据迁移性测试
- MySQL配置文件
- 将数值转换成最接近的某数的整数倍
- mysql的字符串函数
- sybase Error 631 处理方法
- 查看和设置MySQL数据库字符集
- mysql profile参数
- mysql错误码标识含义
- MYSQL 字符集问题
- 如何修改mysql root密码?
- MYSQL学习笔记-c开发环境搭建
- MYSQL学习笔记-存储过程
- mysql存储过程学习总结-操作符
- 一些mysql dba变量监控小程序
- Table Cache相关的优化
- mysql除法精度
- mysql存储过程基本函数
- MYSQL服务器的安装和管理
- mysql 赋予用户权限 grant命令
- mysql不同数据库也不要重复给相同用
- mysql生成假MAC地址的存储过程
- MySQL执行计划
- 安装mysql出现:error 1405 解决方
- mysql存储过程权限
- mysql grant创建用户权限
- mysql去除特殊asc码
- Access二级视频教程
- 平面设计视频教程
- 编程开发视频教程
- 三维动画视频教程
- 网页设计视频教程
- Flash动画视频教程
- 办公软件视频教程
- 网店技巧视频教程
- 硬件技术视频教程
- 等级考试视频教程