MySQL5.7在JSON解析后丢失小数部分的Bug

在MySQL Bugs上提交了 https://bugs.mysql.com/bug.php?id=84935 . 已经在MySQL8.0.1中修复

重现步骤

-- Prepare the table and populate it with records

CREATE TABLE `voucher` (
  `id` varchar(32) NOT NULL COMMENT 'ID',
  `vals` mediumtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `voucher` VALUES ('1', '{"period":90,"amount":5.45}');
INSERT INTO `voucher` VALUES ('2', '{"period":90,"amount":3.99}');

-- Execute queries

mysql> SELECT vals FROM voucher WHERE id='1';
+-----------------------------+
| vals                        |
+-----------------------------+
| {"period":90,"amount":5.45} |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT json_extract(vals, '$.amount') FROM voucher WHERE id='1';
+--------------------------------+
| json_extract(vals, '$.amount') |
+--------------------------------+
| 5.45                           |
+--------------------------------+
1 row in set (0.00 sec)

-- The result of this one is not correct
mysql> SELECT SUM(json_extract(vals, '$.amount')) FROM voucher WHERE id='1'; 
+-------------------------------------+
| SUM(json_extract(vals, '$.amount')) |
+-------------------------------------+
|                                   5 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ROUND(SUM(json_extract(vals, '$.amount')), 2) FROM voucher WHERE id='1';
+-----------------------------------------------+
| ROUND(SUM(json_extract(vals, '$.amount')), 2) |
+-----------------------------------------------+
|                                          5.45 |
+-----------------------------------------------+
1 row in set (0.00 sec)

  

原文地址:https://www.cnblogs.com/milton/p/9976881.html