阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

手机号码,快捷登录

老司机
查看: 1552|回复: 0

[Mysql] Result of DIV with Decimal and Integer 重要bug

[复制链接]
  • TA的每日心情
    开心
    2021-8-30 00:00
  • 签到天数: 35 天

    [LV.5]常住居民I

    发表于 2012-5-1 18:37:26 | 显示全部楼层 |阅读模式

    最近几天在使用php+mysql 设置一个程序时,发现一个奇怪的问题

    在mysql里面使用div,也就是取模函数
    900 div 500  结果=1


    900.00 div 500 结果居然等2
    接着测试
    749.00 div 500 =1
    750.00 div 500 =2


    天这是什么逻辑,让我反复检查了近一个小时的程序代码,发现居然错在这里。
    我的mysql版本是:5.5.12,看官方的说明,似乎是5.5.16以后版本才修复了这个bug

    下面是官方资料:
    [28 Jun 2011 15:54] Jun Yao
    Description:(5 DIV 2) and (5.0 DIV 2) results in different values.  This is not the same behavior in MySQL 5.0 version, in which results seem to make more sense.How to repeat:SELECT 5 DIV 2;Result = 2SELECT 5.0 DIV 2;Result = 3Suggested fix:Since the documentation states that DIV is similar to FLOOR, after the decimal division, maybe FLOOR should be used instead of rounding to the nearest integer.
    [28 Jun 2011 16:00] Jun Yao
    In 5.1, results are also consistent.  Both (5.0 div 2) and (5 div 2) equal 2.
    [29 Jun 2011 7:13] Valeriy Kravchuk
    Thank you for the bug report. Verified just as described. Looks like regression comparing to 5.1.x.
    [24 Sep 2011 16:48] Paul DuBois
    Noted in 5.5.16, 5.6.3 changelogs.(5 DIV 2) and (5.0 DIV 2) produced different results (2 versus 3)because the result of the latter expression was not truncated beforeconversion to integer. This differed from the behavior in MySQL 5.0and 5.1. Now both expressions produce 2.


    该贴已经同步到 sunwy的微博
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表