在TiDB中将2位数的浮点字段求和,得到8位浮点数

时间:2018-03-21 09:17:40

标签: php sql sum tidb

我将一些数据从infobright传输到TiDB。 我的PHP代码如:

$sql='delete from xxx where xxx';
doQuery($sql);
$sql='insert into xxx (...)';
doQuery($sql);

我插入了48595条记录,但它们的总和是8位浮点数。 同时,该字段定义为float(10,2):

mysql> SELECT COUNT(*) FROM adpay;
+----------+
| COUNT(*) |
+----------+
|    48595 | 
+----------+
1 row in set (0.28 sec)

mysql> SELECT SUM(t.spends) FROM (SELECT spends FROM adpay LIMIT 90000) t;
+---------------+
| SUM(t.spends) |
+---------------+
|   42583533.50 | 
+---------------+
1 row in set (0.37 sec)

mysql> SELECT SUM(spends) FROM adpay;
+-------------------+
| SUM(spends)       |
+-------------------+
| 42583533.50033116 | 
+-------------------+
1 row in set (0.35 sec)

mysql> show create table adpay;
...
CREATE TABLE `adpay` (
  `date` date NOT NULL DEFAULT '0000-00-00',
  `adname` varchar(50) NOT NULL DEFAULT '',
  `country` char(10) NOT NULL DEFAULT '',
  `pf` char(20) NOT NULL DEFAULT '',
  `paydate` date NOT NULL DEFAULT '0000-00-00',
  `num` int(10) DEFAULT NULL,
  `spends` float(10,2) NOT NULL,
  `todaynum` int(11) DEFAULT '0',
  `todayspends` float(10,2) DEFAULT '0.00',
  UNIQUE KEY `sdate` (`date`,`adname`,`country`,`pf`,`paydate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

那么,是否存在TiDB的错误,或者我做错了什么?任何建议都表示赞赏。

更新:

mysql> show create table test;
+-------+------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `t` float(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin | 
+-------+------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)''

INSERT INTO test VALUES (1.11);
INSERT INTO test VALUES (1.111);


mysql> SELECT t,t-1.11 FROM test;
+------+----------------------------+
| t    | t-1.11                     |
+------+----------------------------+
| 1.11 | 0.000000014305114648394124 | 
| 1.11 | 0.000000014305114648394124 | 
| 1.11 | 0.000000014305114648394124 | 
+------+----------------------------+
3 rows in set (0.01 sec)

更新2:

我只使用float,没有指定数字。

mysql> show create table test1;
+-------+-------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                          |
+-------+-------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
  `t` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin | 
+-------+-------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> INSERT INTO test1 VALUES (1.11);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test1 VALUES (1.111);
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> SELECT COUNT(*) FROM test1  WHERE t>ROUND(t, 2) ;
+----------+
| COUNT(*) |
+----------+
|        4 | 
+----------+
1 row in set (0.00 sec)

mysql> 
mysql> SELECT t,t-1.11 FROM test1;
+-------+----------------------------+
| t     | t-1.11                     |
+-------+----------------------------+
|  1.11 | 0.000000014305114648394124 | 
| 1.111 |      0.0009999418258666015 | 
|  1.11 | 0.000000014305114648394124 | 
| 1.111 |      0.0009999418258666015 | 
+-------+----------------------------+
4 rows in set (0.00 sec)

更新3:

此表包含不同类型的字段,所有字段都有2位数字。

然后我为每个字段插入1.11和1.111。

只有浮点字段才会将数据作为TiDB中的8位数字

mysql> create table test2(f float(10,2),db double(10,2), de decimal(10,2));
Query OK, 0 rows affected (1.01 sec)

mysql> insert into test2(f,db,de) values(1.11,1.11,1.11);     
Query OK, 1 row affected (0.01 sec)

mysql> insert into test2(f,db,de) values(1.111,1.111,1.111);        
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+----------------------------+---------+---------+
| f    | db   | de   | f-1.11                     | db-1.11 | de-1.11 |
+------+------+------+----------------------------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 |       0 |       0 | 
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 |       0 |       0 | 
+------+------+------+----------------------------+---------+---------+
2 rows in set (0.00 sec)

对于本地mysql 5.6中的所有3个字段都可以:

mysql> create table test2(f float(10,2),db double(10,2), de decimal(10,2));
Query OK, 0 rows affected (0.29 sec)

mysql> insert into test2(f,db,de) values(1.11,1.11,1.11);
Query OK, 1 row affected (0.06 sec)

mysql> insert into test2(f,db,de) values(1.111,1.111,1.111);
Query OK, 1 row affected, 1 warning (0.04 sec)

mysql> select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+--------+---------+---------+
| f    | db   | de   | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+--------+---------+---------+
| 1.11 | 1.11 | 1.11 |   0.00 |    0.00 |    0.00 |
| 1.11 | 1.11 | 1.11 |   0.00 |    0.00 |    0.00 |
+------+------+------+--------+---------+---------+
2 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

问题是由函数summinus的不正确类型推断引起的。我们很快就会解决它。 如果您在使用TiDB时遇到任何问题,可以在下次https://github.com/pingcap/tidb/issues中创建问题。 ^ _ ^

答案 1 :(得分:0)

您可以使用ROUND或FORMAT功能:

SELECT ROUND(SUM(spends), 2) FROM adpay;