相关文章推荐
爱玩的遥控器  ·  福建省公安厅关于印发《福建省公安机关便民惠企 ...·  6 月前    · 
淡定的胡萝卜  ·  《斗罗大陆》小舞换上超短裙,史莱克三美组合出 ...·  1 年前    · 
玉树临风的野马  ·  宝石争霸 - 搜狗百科·  2 年前    · 
爱搭讪的开水瓶  ·  拥有U盾同级别安全性的 ...·  2 年前    · 
温暖的芒果  ·  18个彩蛋,帮你看懂《新蝙蝠侠》_腾讯新闻·  2 年前    · 
小百科  ›  MySQL :: MySQL 8.0 Reference Manual :: 12.24.5 Precision Math Examples
mysql select exact
温柔的烤红薯
2 年前
Argument Handling by Spatial Functions
Functions That Create Geometry Values from WKT Values
Functions That Create Geometry Values from WKB Values
MySQL-Specific Functions That Create Geometry Values
Geometry Format Conversion Functions
Geometry Property Functions
General Geometry Property Functions
Point Property Functions
LineString and MultiLineString Property Functions
Polygon and MultiPolygon Property Functions
GeometryCollection Property Functions
Function which Configures Group Replication Primary
Functions which Configure the Group Replication Mode
Functions to Inspect and Configure the Maximum Consensus Instances of a Group
Functions to Inspect and Set the Group Replication Communication Protocol Version
Functions to Set and Reset Group Replication Member Actions
Functions Used with Global Transaction Identifiers (GTIDs)
Asynchronous Replication Channel Failover Functions
Position-Based Synchronization Functions
This section provides some examples that show precision math query results in MySQL. These examples demonstrate the principles described in Section 12.24.3, “Expression Handling” , and Section 12.24.4, “Rounding Behavior” . Example 1 . Numbers are used with their exact value as given when possible:

mysql> SELECT (.1 + .2) = .3;
+----------------+
| (.1 + .2) = .3 |
+----------------+
|              1 |
+----------------+

For floating-point values, results are inexact:

mysql> SELECT (.1E0 + .2E0) = .3E0;
+----------------------+
| (.1E0 + .2E0) = .3E0 |
+----------------------+
|                    0 |
+----------------------+

Another way to see the difference in exact and approximate value handling is to add a small number to a sum many times. Consider the following stored procedure, which adds .0001 to a variable 1,000 times.

CREATE PROCEDURE p ()
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE d DECIMAL(10,4) DEFAULT 0;
  DECLARE f FLOAT DEFAULT 0;
  WHILE i < 10000 DO
    SET d = d + .0001;
    SET f = f + .0001E0;
    SET i = i + 1;
  END WHILE;
  SELECT d, f;
      The sum for both d and f
      logically should be 1, but that is true only for the decimal
      calculation. The floating-point calculation introduces small
      errors:
    

+--------+------------------+
| d      | f                |
+--------+------------------+
| 1.0000 | 0.99999999999991 |
+--------+------------------+

Example 2. Multiplication is performed with the scale required by standard SQL. That is, for two numbers X1 and X2 that have scale S1 and S2, the scale of the result is S1 + S2:

mysql> SELECT .01 * .01;
+-----------+
| .01 * .01 |
+-----------+
| 0.0001    |
+-----------+

Example 3. Rounding behavior for exact-value numbers is well-defined: Rounding behavior (for example, with the ROUND() function) is independent of the implementation of the underlying C library, which means that results are consistent from platform to platform. Rounding for exact-value columns (DECIMAL and integer) and exact-valued numbers uses the “round half away from zero” rule. A value with a fractional part of .5 or greater is rounded away from zero to the nearest integer, as shown here:

mysql> SELECT ROUND(2.5), ROUND(-2.5);
+------------+-------------+
| ROUND(2.5) | ROUND(-2.5) |
+------------+-------------+
| 3          | -3          |
+------------+-------------+
  • Rounding for floating-point values uses the C library, which on many systems uses the “round to nearest even” rule. A value with a fractional part exactly half way between two integers is rounded to the nearest even integer:

    mysql> SELECT ROUND(2.5E0), ROUND(-2.5E0);
    +--------------+---------------+
    | ROUND(2.5E0) | ROUND(-2.5E0) |
    +--------------+---------------+
    |            2 |            -2 |
    +--------------+---------------+
  • Example 4. In strict mode, inserting a value that is out of range for a column causes an error, rather than truncation to a legal value. When MySQL is not running in strict mode, truncation to a legal value occurs:

    mysql> SET sql_mode='';
    Query OK, 0 rows affected (0.00 sec)
    mysql> CREATE TABLE t (i TINYINT);
    Query OK, 0 rows affected (0.01 sec)
    mysql> INSERT INTO t SET i = 128;
    Query OK, 1 row affected, 1 warning (0.00 sec)
    mysql> SELECT i FROM t;
    +------+
    | i    |
    +------+
    |  127 |
    +------+
    1 row in set (0.00 sec)

    However, an error occurs if strict mode is in effect:

    mysql> SET sql_mode='STRICT_ALL_TABLES';
    Query OK, 0 rows affected (0.00 sec)
    mysql> CREATE TABLE t (i TINYINT);
    Query OK, 0 rows affected (0.00 sec)
    mysql> INSERT INTO t SET i = 128;
    ERROR 1264 (22003): Out of range value adjusted for column 'i' at row 1
    mysql> SELECT i FROM t;
    Empty set (0.00 sec)

    Example 5: In strict mode and with ERROR_FOR_DIVISION_BY_ZERO set, division by zero causes an error, not a result of NULL. In nonstrict mode, division by zero has a result of NULL:

    mysql> SET sql_mode='';
    Query OK, 0 rows affected (0.01 sec)
    mysql> CREATE TABLE t (i TINYINT);
    Query OK, 0 rows affected (0.00 sec)
    mysql> INSERT INTO t SET i = 1 / 0;
    Query OK, 1 row affected (0.00 sec)
    mysql> SELECT i FROM t;
    +------+
    | i    |
    +------+
    | NULL |
    +------+
    1 row in set (0.03 sec)

    However, division by zero is an error if the proper SQL modes are in effect:

    mysql> SET sql_mode='STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO';
    Query OK, 0 rows affected (0.00 sec)
    mysql> CREATE TABLE t (i TINYINT);
    Query OK, 0 rows affected (0.00 sec)
    mysql> INSERT INTO t SET i = 1 / 0;
    ERROR 1365 (22012): Division by 0
    mysql> SELECT i FROM t;
    Empty set (0.01 sec)

    Example 6. Exact-value literals are evaluated as exact values. Approximate-value literals are evaluated using floating point, but exact-value literals are handled as DECIMAL:

    mysql> CREATE TABLE t SELECT 2.5 AS a, 25E-1 AS b;
    Query OK, 1 row affected (0.01 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    mysql> DESCRIBE t;
    +-------+-----------------------+------+-----+---------+-------+
    | Field | Type                  | Null | Key | Default | Extra |
    +-------+-----------------------+------+-----+---------+-------+
    | a     | decimal(2,1) unsigned | NO   |     | 0.0     |       |
    | b     | double                | NO   |     | 0       |       |
    +-------+-----------------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)

    Example 7. If the argument to an aggregate function is an exact numeric type, the result is also an exact numeric type, with a scale at least that of the argument. Consider these statements:

    mysql> CREATE TABLE t (i INT, d DECIMAL, f FLOAT);
    mysql> INSERT INTO t VALUES(1,1,1);
    mysql> CREATE TABLE y SELECT AVG(i), AVG(d), AVG(f) FROM t;

    The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type:

    mysql> DESCRIBE y;
    +--------+---------------+------+-----+---------+-------+
    | Field  | Type          | Null | Key | Default | Extra |
    +--------+---------------+------+-----+---------+-------+
    | AVG(i) | decimal(14,4) | YES  |     | NULL    |       |
    | AVG(d) | decimal(14,4) | YES  |     | NULL    |       |
    | AVG(f) | double        | YES  |     | NULL    |       |
    +--------+---------------+------+-----+---------+-------+

    The result is a double only for the floating-point argument. For exact type arguments, the result is also an exact type.

     
    推荐文章
    爱玩的遥控器  ·  福建省公安厅关于印发《福建省公安机关便民惠企助力高质量发展18项措施细化落实方案及责任分解》的通知 _ 现行有效 _ 省公安厅
    6 月前
    淡定的胡萝卜  ·  《斗罗大陆》小舞换上超短裙,史莱克三美组合出道,满屏大长腿_宁荣荣_朱竹清_眼睫毛
    1 年前
    玉树临风的野马  ·  宝石争霸 - 搜狗百科
    2 年前
    爱搭讪的开水瓶  ·  拥有U盾同级别安全性的 SIM盾让你的资金多一个安全保障_财富号_东方财富网
    2 年前
    温暖的芒果  ·  18个彩蛋,帮你看懂《新蝙蝠侠》_腾讯新闻
    2 年前
    今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
    删除内容请联系邮箱 2879853325@qq.com
    小百科 - 百科知识指南
    © 2024 ~ 沪ICP备11025650号