使用timediff减去时间

时间:2013-12-24 13:26:50

标签: mysql

帮助。我在mysql中有这个查询。它的作用是组合三个表中的字段。一个字段(持续时间(h))它减去结束时间和开始时间。可能时间数据仅为时间而非日期时间。所以我决定使用timediff功能。这在同一天内运行良好。但不是时间差异,如00:00 - 23:30;在这种情况下,我得到答案-23.5。我该怎么做才能纠正这个问题。提前谢谢。

SELECT `tblproductiondata`.`productionDay` , `tbllinestoppagecategories`.`category` , `tblshifts`.`ID` , `tblstoppagedescriptions`.`endTime` , `tblstoppagedescriptions`.`startTime` , IFNULL( ROUND( `tblstoppagedescriptions`.`duration(mins)` /60, 2 ) , ROUND( (
TIME_TO_SEC( TIMEDIFF( `tblstoppagedescriptions`.`endTime` , `tblstoppagedescriptions`.`startTime` ) ) /3600 ) , 2 )
) AS `Duration (h)`
FROM (
`tbllinestoppagecategories`
INNER JOIN `tblstoppagereasons` ON `tbllinestoppagecategories`.`categoryID` = `tblstoppagereasons`.`stoppagecategory`
)
INNER JOIN (
`tblshifts`
INNER JOIN (
`tblproductiondata`
INNER JOIN `tblstoppageDescriptions` ON `tblproductiondata`.`productionID` = `tblstoppagedescriptions`.`prodDate`
) ON `tblshifts`.`ID` = `tblproductiondata`.`shiftName`
) ON `tblstoppageReasons`.`resID` = `tblstoppagedescriptions`.`stoppageReason`
WHERE (
(
(
`tbllinestoppagecategories`.`category`
) <> "Changeover Maintenance Activities"
)
)
ORDER BY `tblproductiondata`.`productionDay` ASC

1 个答案:

答案 0 :(得分:0)

TIME_TO_SEC(
    TIMEDIFF( 
        CONCAT(tblproductiondata.productionDay, ' ', tblstoppagedescriptions.endTime),  
        CONCAT(tblproductiondata.productionDay, ' ', `tblstoppagedescriptions`.`startTime`)
    )
)