2013-11-29 88 views
0

真的需要一些幫助。SQL中的時差。午夜總時間

有與下列的表

 
startdate, stopdate, starttime, stoptime, totaltime 

他們在格式YYYY-MM-DDHH-MM

我如何獲得totaltimeHH-MM格式(有時啓動和停止通過午夜)

謝謝

回答

0

使用TIMEDIFF將返回00:00:00.00000 //這種格式

TIMEDIFF('2013-11-20 12:01:00', '2010-11-19 12:00:00') 

,或者你可以隱蔽到秒

TIME_TO_SEC(TIMEDIFF('2013-11-20 12:01:00', '2010-11-19 12:00:00')) 
0

嘗試這種方式

UPDATE table1 
    SET totaltime = TIMEDIFF(ADDTIME(stopdate, stoptime), 
          ADDTIME(startdate, starttime)); 

這裏是SQLFiddle演示

更新:這是一個午夜時間的例子。

 
mysql> CREATE TABLE Table2 
    -> (`startdate` date, 
    -> `stopdate` date, 
    -> `starttime` time, 
    -> `stoptime` time, 
    -> `totaltime` time); 
Query OK, 0 rows affected (0.02 sec) 

mysql> INSERT INTO Table2 VALUES 
    -> ('2013-11-27', '2013-11-28', '11:00', '00:00', NULL), 
    -> ('2013-11-27', '2013-11-28', '00:00', '15:00', NULL), 
    -> ('2013-11-27', '2013-11-28', '00:00', '00:00', NULL); 
Query OK, 3 rows affected (0.01 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

mysql> UPDATE table2 
    -> SET totaltime = TIMEDIFF(ADDTIME(stopdate, stoptime), 
    ->        ADDTIME(startdate, starttime)); 
Query OK, 3 rows affected (0.01 sec) 
Rows matched: 3 Changed: 3 Warnings: 0 

mysql> SELECT * FROM table2; 
+------------+------------+-----------+----------+-----------+ 
| startdate | stopdate | starttime | stoptime | totaltime | 
+------------+------------+-----------+----------+-----------+ 
| 2013-11-27 | 2013-11-28 | 11:00:00 | 00:00:00 | 13:00:00 | 
| 2013-11-27 | 2013-11-28 | 00:00:00 | 15:00:00 | 39:00:00 | 
| 2013-11-27 | 2013-11-28 | 00:00:00 | 00:00:00 | 24:00:00 | 
+------------+------------+-----------+----------+-----------+ 
3 rows in set (0.00 sec) 

+0

此更新爲我工作不錯,但在經過午夜的情況下,我得到負的總 – user3048250

+0

@ user3048250使用'timeDiff測量(later_date_time,former_date_time)'取得積極的結果。 –

+0

奇怪的事情發生時,通過00.00時,它將從開始時間減去停止時間並使其爲負數。 – user3048250