MySQL Average Date When Dates are Higher than 2038-01-19 (Can't Use UNIX_TIMESTAMP)
I need to find the average date in a MySQL DB column. The dates will be above 2038-01-19 so I cannot use the UNIX_TIMESTAMP function. Doe开发者_StackOverflows anyone know of a different way to get the average date?
Why not just use the AVG
function?
create table test (mydate date);
insert into test values ('2042-10-01'), ('2043-10-01'), ('2044-10-01');
mysql> select avg(mydate) from test;
+-------------+
| avg(mydate) |
+-------------+
| 20431001 |
+-------------+
1 row in set (0.00 sec)
Ok. Instead of using the UNIX_TIMESTAMP I used TO_DAYS and FROM_DAYS and it seems to work as expected.
create table test (mydate date);
insert into test values ('2042-10-01'), ('2043-10-01'), ('2044-10-01');
mysql > SELECT FROM_DAYS(AVG(TO_DAYS(mydate))) FROM test;
+---------------------------------+
| FROM_DAYS(AVG(TO_DAYS(mydate))) |
+---------------------------------+
| 2043-10-01 |
+---------------------------------+
精彩评论