结合SUB_DATE和TIMEDIFF在mysql中减去1小时

时间:2013-07-25 12:15:51

标签: mysql date

我遇到了timediffDATE_SUB的问题。这是我的MYSQL查询:

SELECT id, clock_user_id, clock_date,

(Select clock_time from aura_clock where aura_clock.clock_type = 'Start' and  
 aura_clock.clock_date = t1.clock_date) as start, (Select clock_time 
 from aura_clock where aura_clock.clock_type = 'Stop' 
 and aura_clock.clock_date =   
 t1.clock_date) as Stop,

问题从这里开始

TIMEDIFF((select clock_time FROM aura_clock t 
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time 
ORDER BY t.clock_time LIMIT 1), MIN(clock_time)) as spent

FROM aura_clock t1 WHERE t1.clock_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 
WEEKDAY(CURDATE()) DAY),INTERVAL 15 day) 
AND t1.clock_date < DATE_SUB(curdate(),INTERVAL DAYOFWEEK(curdate()) + 6 day)

GROUP BY clock_date

结果是:

enter image description here

现在,我想从使用DATE_SUB所花费的时间减去1小时,但它不起作用。

1 个答案:

答案 0 :(得分:2)

mirkobrankovic 写道:

((TIMEDIFF((select clock_time FROM aura_clock t 
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time 
ORDER BY t.clock_time LIMIT 1), MIN(clock_time))) - INTERVAL 1 HOUR) AS new_spent

应该有效

编辑:

我得到的最好的是几秒钟的时间: http://sqlfiddle.com/#!2/18160/66/0

很大程度上取决于MYSQL版本