2016-12-16 50 views
0

下面提到的查詢返回空,但我內心的查詢取像 shown hereMySQL的時間日期轉換取空值

SELECT CONCAT(
FLOOR(HOUR(time_milis)/24), ' ', 
MOD(HOUR(time_milis), 24), ':', 
MINUTE(time_milis),':', 
SECOND(time_milis)) 
FROM (
    SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id 
)se 

值爲什麼不使用DATE_FORMAT()這個東西我在這裏缺少

+1

[HOUR()](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour)在工作時間上不整數 – bansi

回答

0

time_milis都是秒,但hour需要time'10:05:03',你可以只使用sec_to_time。就像這樣:

SELECT concat(hour(sec_to_time(time_milis)), ' ', sec_to_time(time_milis)) 
FROM (
    SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id 
)se 
1

?所以我的猜測是這樣的:

SELECT login_id , 
    DATE_FORMAT(
    SUM(
     TIMEDIFF(
      logout_timestamp,login_timestamp) 
     ) 
    ) as some_time 
FROM t_access_log 
WHERE logout_timestamp!='0000-00-00 00:00:00' 
GROUP BY login_id; 
+0

'DATE_FORMAT()'似乎在'date'上工作? – Blank