MariaDB/MySQL TO_秒和聚合函数

MariaDB/MySQL TO_秒和聚合函数,mysql,aggregate,mariadb,Mysql,Aggregate,Mariadb,我想使用聚合函数(AVG,COUNT)来汇总我的表。然而,结果并不是我所期望的。下面是一个示例表: MariaDB [test]> select * from mytable; +----+---------------------+------+ | id | ts | val | +----+---------------------+------+ | 1 | 2016-01-01 01:02:03 | 1 | | 2 | 2016-01

我想使用聚合函数(AVG,COUNT)来汇总我的表。然而,结果并不是我所期望的。下面是一个示例表:

MariaDB [test]> select * from mytable;
+----+---------------------+------+
| id | ts                  | val  |
+----+---------------------+------+
|  1 | 2016-01-01 01:02:03 |    1 |
|  2 | 2016-01-01 01:02:04 |    2 |
|  3 | 2016-01-01 01:02:04 |    3 |
|  4 | 2016-01-01 01:02:05 |    4 |
|  5 | 2016-01-01 01:02:05 |    5 |
+----+---------------------+------+
查询#1(确定):

查询#2(?):

预期结果:

+-------------+------+
| tsec        | mval |
+-------------+------+
| 63618829323 |    1 |
| 63618829324 |  2.5 |
| 63618829325 |  4.5 |
+-------------+------+
SQL小提琴:
MariaDB版本>mysql版本15.1发行版10.1.17-MariaDB,适用于Linux(x86_64),使用readline 5.1

当然,我可以使用其他日期/时间函数(UNIX\u TIMESTAMP等)来执行该任务。然而,我想知道为什么结果不同

我错过了什么?我是否误解了TO_SECONDS的用法?

这是一个奇怪的数据类型问题。以下方法确实有效:

select cast(to_seconds(ts) as decimal(20, 0)) as tsec, avg(val)
from mytable
group by tsec;

我不知道为什么选择时
to_seconds()
的返回值会大到足以存储该值,但在使用
group by
时会转换为整数。我无法回答为什么会发生这种情况,但请确认MySQL版本中仍然会发生这种情况。5.7.32,并建议一个(笨拙的)解决方法

例如:

create table Foo (t datetime);
insert into Foo values('2020-01-01 00:00:01');
insert into Foo values('2020-01-02 00:00:01');
insert into Foo values('2020-01-03 00:00:01');
select t, to_seconds(t) from Foo;
+---------------------+---------------+
| t                   | to_seconds(t) |
+---------------------+---------------+
| 2020-01-01 00:00:01 |   63745056001 |
| 2020-01-02 00:00:01 |   63745142401 |
| 2020-01-03 00:00:01 |   63745228801 |
+---------------------+---------------+
到目前为止还不错。现在,使用
分组依据

select t, to_seconds(t) from Foo group by t;
+---------------------+---------------+
| t                   | to_seconds(t) |
+---------------------+---------------+
| 2020-01-01 00:00:01 |    2147483647 |
| 2020-01-02 00:00:01 |    2147483647 |
| 2020-01-03 00:00:01 |    2147483647 |
+---------------------+---------------+
每行的
to_seconds(t)
值变为2147483647,等于2^31-1,即可由有符号4字节整数表示的最大正值。这看起来像是MySQL服务器内部的某种数据转换问题,这种情况不应该发生

2019年3月,MySQL bug报告了本质上相同的问题( ),但此后,除了确认之外,没有任何活动添加到该错误报告中

有趣的是,如果我们使用
而不是
to_seconds(t)
max(to_seconds(t))
来代替
to_seconds(t)
(理论上,这应该会产生相同的结果,因为我们是在
t
上分组的):


这似乎是一个数据类型问题。返回值被视为一个整数而不是一个大整数。感谢您的快速回答。
create table Foo (t datetime);
insert into Foo values('2020-01-01 00:00:01');
insert into Foo values('2020-01-02 00:00:01');
insert into Foo values('2020-01-03 00:00:01');
select t, to_seconds(t) from Foo;
+---------------------+---------------+
| t                   | to_seconds(t) |
+---------------------+---------------+
| 2020-01-01 00:00:01 |   63745056001 |
| 2020-01-02 00:00:01 |   63745142401 |
| 2020-01-03 00:00:01 |   63745228801 |
+---------------------+---------------+
select t, to_seconds(t) from Foo group by t;
+---------------------+---------------+
| t                   | to_seconds(t) |
+---------------------+---------------+
| 2020-01-01 00:00:01 |    2147483647 |
| 2020-01-02 00:00:01 |    2147483647 |
| 2020-01-03 00:00:01 |    2147483647 |
+---------------------+---------------+
 select t, to_seconds(t), max(t), to_seconds(max(t)), max(to_seconds(t)) from Foo group by t;
+---------------------+---------------+---------------------+--------------------+--------------------+
| t                   | to_seconds(t) | max(t)              | to_seconds(max(t)) | max(to_seconds(t)) |
+---------------------+---------------+---------------------+--------------------+--------------------+
| 2020-01-01 00:00:01 |    2147483647 | 2020-01-01 00:00:01 |        63745056001 |        63745056001 |
| 2020-01-02 00:00:01 |    2147483647 | 2020-01-02 00:00:01 |        63745142401 |        63745142401 |
| 2020-01-03 00:00:01 |    2147483647 | 2020-01-03 00:00:01 |        63745228801 |        63745228801 |
+---------------------+---------------+---------------------+--------------------+--------------------+