为了解决这个问题,可以转换成数字单元,执行聚合操作,转换回一个时间值。
例子,mysql sum函数与avg函数。
sql语句,如下:
--创建表
mysql> create table employee(
-> id int,
-> first_name varchar(15),
-> last_name varchar(15),
-> start_date date,
-> end_date date,
-> salary float(8,2),
-> city varchar(10),
-> description varchar(15)
-> );
query ok, 0 rows affected (0.16 sec)
--添加数据
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values (1,'jason', 'martin', '19960725', '20060725', 1234.56, 'toronto', 'programmer');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(2,'alison', 'mathews', '19760321', '19860221', 6661.78, 'vancouver','tester');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(3,'james', 'smith', '19781212', '19900315', 6544.78, 'vancouver','tester');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(4,'celia', 'rice', '19821024', '19990421', 2344.78, 'vancouver','manager');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(5,'robert', 'black', '19840115', '19980808', 2334.78, 'vancouver','tester');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(6,'linda', 'green', '19870730', '19960104', 4322.78,'new york', 'tester');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(7,'david', 'larry', '19901231', '19980212', 7897.78,'new york', 'manager');
query ok, 1 row affected (0.00 sec)
mysql> insert into employee(id,first_name, last_name, start_date, end_date, salary, city, description)
-> values(8,'james', 'cat', '19960917', '20020415', 1232.78,'vancouver', 'tester');
query ok, 1 row affected (0.00 sec)
mysql> select * from employee;
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| id | first_name | last_name | start_date | end_date | salary | city | description |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| 1 | jason | martin | 1996-07-25 | 2006-07-25 | 1234.56 | toronto | programmer |
| 2 | alison | mathews | 1976-03-21 | 1986-02-21 | 6661.78 | vancouver | tester |
| 3 | james | smith | 1978-12-12 | 1990-03-15 | 6544.78 | vancouver | tester |
| 4 | celia | rice | 1982-10-24 | 1999-04-21 | 2344.78 | vancouver | manager |
| 5 | robert | black | 1984-01-15 | 1998-08-08 | 2334.78 | vancouver | tester |
| 6 | linda | green | 1987-07-30 | 1996-01-04 | 4322.78 | new york | tester |
| 7 | david | larry | 1990-12-31 | 1998-02-12 | 7897.78 | new york | manager |
| 8 | james | cat | 1996-09-17 | 2002-04-15 | 1232.78 | vancouver | tester |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
8 rows in set (0.00 sec)
--mysql sum函数
mysql> select from_days(sum(to_days(start_date))) from employee;
+-------------------------------------+
| from_days(sum(to_days(start_date))) |
+-------------------------------------+
| 0000-00-00 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> drop table employee;
query ok, 0 rows affected (0.00 sec)