0

我了解无法直接对别名执行计算,但是有没有办法可以找到别名的总和(总时间)?我在这里先向您的帮助表示感谢。

    SELECT *,
         TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ,
         ADDTIME( TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ) ,
         TIME_FORMAT( ADDTIME( TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ) , '%H:%i' )
    AS totaltime
        SUM(SELECT(totaltime)) AS sumtotal
    FROM
        timer
    WHERE
        groupid='100'
    AND
        MONTH(`date`) = MONTH(NOW())
    GROUP BY userid
4

1 回答 1

0

只需添加另一层抽象;)

SELECT TIME_FORMAT(sec_to_time(sum(timecard.totaltime)),'%H:%i' ) FROM 
(
SELECT *,
    TIMEDIFF( out_time, in_time ) , 
    TIMEDIFF( lunch_in, lunch_out ) , 
    ADDTIME( TIMEDIFF( out_time, in_time ) , TIMEDIFF( lunch_in, lunch_out ) ) , 
    ADDTIME( TIMEDIFF( out_time, in_time ) , 
    TIMEDIFF( lunch_in, lunch_out ) )
AS totaltime        
FROM
    timecard
WHERE
    comp_id='129'
AND
    MONTH(`date`) = MONTH(NOW())
GROUP BY emp_id
) timecard

小提琴链接:http ://sqlfiddle.com/#!2/b33d8/16/0

于 2013-08-14T21:31:53.480 回答