<small id='IQaWb'></small><noframes id='IQaWb'>

  • <tfoot id='IQaWb'></tfoot>
    • <bdo id='IQaWb'></bdo><ul id='IQaWb'></ul>

    <i id='IQaWb'><tr id='IQaWb'><dt id='IQaWb'><q id='IQaWb'><span id='IQaWb'><b id='IQaWb'><form id='IQaWb'><ins id='IQaWb'></ins><ul id='IQaWb'></ul><sub id='IQaWb'></sub></form><legend id='IQaWb'></legend><bdo id='IQaWb'><pre id='IQaWb'><center id='IQaWb'></center></pre></bdo></b><th id='IQaWb'></th></span></q></dt></tr></i><div id='IQaWb'><tfoot id='IQaWb'></tfoot><dl id='IQaWb'><fieldset id='IQaWb'></fieldset></dl></div>

    <legend id='IQaWb'><style id='IQaWb'><dir id='IQaWb'><q id='IQaWb'></q></dir></style></legend>

      1. MySQL浮点比较问题

        MySQL floating point comparison issues(MySQL浮点比较问题)
        • <tfoot id='rRTDi'></tfoot>
          <i id='rRTDi'><tr id='rRTDi'><dt id='rRTDi'><q id='rRTDi'><span id='rRTDi'><b id='rRTDi'><form id='rRTDi'><ins id='rRTDi'></ins><ul id='rRTDi'></ul><sub id='rRTDi'></sub></form><legend id='rRTDi'></legend><bdo id='rRTDi'><pre id='rRTDi'><center id='rRTDi'></center></pre></bdo></b><th id='rRTDi'></th></span></q></dt></tr></i><div id='rRTDi'><tfoot id='rRTDi'></tfoot><dl id='rRTDi'><fieldset id='rRTDi'></fieldset></dl></div>

              • <bdo id='rRTDi'></bdo><ul id='rRTDi'></ul>

                  <tbody id='rRTDi'></tbody>

                • <small id='rRTDi'></small><noframes id='rRTDi'>

                  <legend id='rRTDi'><style id='rRTDi'><dir id='rRTDi'><q id='rRTDi'></q></dir></style></legend>
                • 本文介绍了MySQL浮点比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 MySQL 数据库架构中引入浮点列时遇到了一个问题,即对浮点值的比较并不总是返回正确的结果.

                  I ran into an issue by introducing floating point columns in the MySQL database schema that the comparisons on floating point values don't return the correct results always.

                  1 - 50.12
                  2 - 34.57
                  3 - 12.75
                  4 - ...(12:00以内休息)

                  1 - 50.12
                  2 - 34.57
                  3 - 12.75
                  4 - ...(rest all less than 12.00)

                  SELECT COUNT(*) FROM `users` WHERE `points` > "12.75"
                  

                  这会返回3".

                  我读过 MySQL 中浮点值的比较是一个坏主意,十进制类型是更好的选择.

                  I have read that the comparisons of floating point values in MySQL is a bad idea and decimal type is the better option.

                  我是否有希望继续使用浮点类型并使比较正常工作?

                  Do I have any hope of moving ahead with the float type and get the comparisons to work correctly?

                  推荐答案

                  你注意到下面的问题了吗?

                  Do you notice the problem below?

                  CREATE TABLE a (num float);
                  
                  INSERT INTO a VALUES (50.12);
                  INSERT INTO a VALUES (34.57);
                  INSERT INTO a VALUES (12.75);
                  INSERT INTO a VALUES (11.22);
                  INSERT INTO a VALUES (10.46);
                  INSERT INTO a VALUES (9.35);
                  INSERT INTO a VALUES (8.55);
                  INSERT INTO a VALUES (7.23);
                  INSERT INTO a VALUES (6.53);
                  INSERT INTO a VALUES (5.15);
                  INSERT INTO a VALUES (4.01);
                  
                  SELECT SUM(num) FROM a;
                  +-----------------+
                  | SUM(num)        |
                  +-----------------+
                  | 159.94000005722 | 
                  +-----------------+
                  

                  在其中一些行之间有一个额外的 0.00000005722 分布.因此,与它们初始化时使用的值相比,其中一些值将返回 false.

                  There's an extra 0.00000005722 spread between some of those rows. Therefore some of those values will return false when compared with the value they were initialized with.

                  为避免浮点运算和比较出现问题,您应该使用 DECIMAL 数据类型:

                  To avoid problems with floating-point arithmetic and comparisons, you should use the DECIMAL data type:

                  ALTER TABLE a MODIFY num DECIMAL(6,2);
                  
                  SELECT SUM(num) FROM a;
                  +----------+
                  | SUM(num) |
                  +----------+
                  |   159.94 | 
                  +----------+
                  1 row in set (0.00 sec)
                  

                  这篇关于MySQL浮点比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)

                  <tfoot id='CnhPh'></tfoot>

                • <legend id='CnhPh'><style id='CnhPh'><dir id='CnhPh'><q id='CnhPh'></q></dir></style></legend>
                    <tbody id='CnhPh'></tbody>

                  <small id='CnhPh'></small><noframes id='CnhPh'>

                      <i id='CnhPh'><tr id='CnhPh'><dt id='CnhPh'><q id='CnhPh'><span id='CnhPh'><b id='CnhPh'><form id='CnhPh'><ins id='CnhPh'></ins><ul id='CnhPh'></ul><sub id='CnhPh'></sub></form><legend id='CnhPh'></legend><bdo id='CnhPh'><pre id='CnhPh'><center id='CnhPh'></center></pre></bdo></b><th id='CnhPh'></th></span></q></dt></tr></i><div id='CnhPh'><tfoot id='CnhPh'></tfoot><dl id='CnhPh'><fieldset id='CnhPh'></fieldset></dl></div>

                            <bdo id='CnhPh'></bdo><ul id='CnhPh'></ul>