0

我将一些数据从 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 中只有 float 字段将数据作为 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)
4

2 回答 2

1

问题是由于函数summinus. 我们会尽快修复它。如果在使用 TiDB 时遇到任何问题,下次可以在https://github.com/pingcap/tidb/issues中创建问题。^_^

于 2018-03-21T10:42:56.430 回答
0

您可以使用 ROUND 或 FORMAT 函数:

SELECT ROUND(SUM(spends), 2) FROM adpay;
于 2018-03-21T09:27:42.887 回答