Sinagaman Eidelbert
Sinagaman Eidelbert

Reputation: 27

MYSQL Select Query with SUM Time

I have following table in mysql:

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

I want to SUM start time and plus time. I need the following output:

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

And the final result, I want to get the only one max value

shp_01   |  
14:30:00 |

so, how to get the final result by using only one query?

Upvotes: 0

Views: 44

Answers (2)

Strawberry
Strawberry

Reputation: 33945

 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 |
 +---------+------------+-----------+----------+

Upvotes: 1

Source Codeplay
Source Codeplay

Reputation: 19

Use the Having clause to specify your condition and get the results as you needed

Upvotes: 1

Related Questions