MYSQL使用SUM Time选择查询

时间:2014-10-30 15:01:41

标签: mysql

我在mysql中有以下表:

 ship_id| start time  | plus time | 
----------------------------------------------
 shp_01 | 14:00:00    | 00:30:00  |  
 shp_02 | 02:00:00    | 00:30:00  | 

我想要SUM开始时间和加时间。 我需要以下输出:

shp_01   | shp_02   |
14:30:00 | 02:30:00 |

最后的结果是,我想得到唯一一个最大值

shp_01   |  
14:30:00 |

那么,如何只使用一个查询来获得最终结果呢?

2 个答案:

答案 0 :(得分:1)

使用Having子句指定条件并根据需要获得结果

答案 1 :(得分:1)

 DROP TABLE my_table;

 CREATE TABLE my_table
 (ship_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,start_time  TIME NOT NULL
 ,plus_time TIME NOT NULL
 );

 INSERT INTO my_table VALUES
 (1 ,'14:00:00','00:30:00'),
 (2 ,'02:00:00','00:30:00');

 SELECT * FROM my_table;
 +---------+------------+-----------+
 | ship_id | start_time | plus_time |
 +---------+------------+-----------+
 |       1 | 14:00:00   | 00:30:00  |
 |       2 | 02:00:00   | 00:30:00  |
 +---------+------------+-----------+

 SELECT *, ADDTIME(start_time,plus_time) total FROM my_table ORDER BY total DESC LIMIT 1;
 +---------+------------+-----------+----------+
 | ship_id | start_time | plus_time | total    |
 +---------+------------+-----------+----------+
 |       1 | 14:00:00   | 00:30:00  | 14:30:00 |
 +---------+------------+-----------+----------+