SQL:总结DATEDIFFs

时间:2012-08-10 09:54:16

标签: mysql

使用mysql

2个日期:start_dateend_date

改变后 TIMEDIFF(start_date, end_date) AS time_diff

我想SUM()所有time_diff所以我试试这个 SUM(TIMEDIFF(start_date, end_date)) AS time_diff

这回归了我不期望的东西 我做错了什么?

修改 SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(end_date, START_DATE)))) AS time_diff

完美无缺

2 个答案:

答案 0 :(得分:0)

end_date应该是第一个而start_date秒,否则你会得到负值作为输出:

SUM(TIME_DIFF(CAST(end_date AS DATETIME), CAST(start_date AS DATETIME))) AS time_diff

答案 1 :(得分:0)

SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(end_date, START_DATE)))) AS time_diff