2015-04-02 44 views
0

我有以下表回空當瑪麗亞計算時間分貝

CREATE TABLE T_WorkContents (
user_id varchar(5), 
working_date DATE NOT NULL, 
working_start_time TIME NOT NULL, 
working_end_time TIME NOT NULL, 
break_time TIME DEFAULT '0', 
holiday_mark tinyint DEFAULT '0', 
overtime_hours TIME DEFAULT '0', 
overtime_reason TEXT, 
work_detail TEXT, 
remarks TEXT, 
regist_user varchar(5), 
regist_date DATETIME, 
update_user varchar(5), 
update_date DATETIME, 
delete_flag tinyint DEFAULT '0', 
PRIMARY KEY(user_id, working_date), 
FOREIGN KEY(user_id) REFERENCES T_UserInfo(user_id) 
); 

CREATE TABLE T_UserInfo (
user_id varchar(5) NOT NULL, 
user_name varchar(30), 
password varchar(20) NOT NULL, 
authority tinyint NOT NULL DEFAULT '1', 
regist_user varchar(5), 
regist_date datetime, 
update_user varchar(5), 
update_date datetime, 
delete_flag tinyint NOT NULL DEFAULT '0', 
PRIMARY KEY(user_id), 
FOREIGN KEY(authority) REFERENCES T_Authority(authority) 
); 

像這樣

SELECT t.user_id, u.user_name, SUM(TIMESTAMPDIFF(HOUR,  TIME(t.working_start_time), TIME(t.working_end_time))) AS total_time, SUM(TIME(TIMESTAMPDIFF(HOUR, TIME(CONVERT('00:00:00',TIME)), TIME(t.overtime_hours)))) AS overtime , SUM(TIME(TIMESTAMPDIFF(HOUR, TIME(CONVERT('00:00:00',TIME)), Time(t.break_time))))AS break_time , COUNT(*) AS count_day ,MONTH(t.working_date) as working_month,YEAR(t.working_date) AS working_year FROM T_WorkContents AS t INNER JOIN T_UserInfo AS u ON u.user_id = t.user_id WHERE t.delete_flag = 0 AND u.delete_flag = 0 AND MONTH(t.working_date) = 4 AND YEAR(t.working_date) = 2015 GROUP BY t.user_id, working_month ,working_year; 

SQL腳本後,我執行該腳本,我得到以下結果

enter image description here

這裏有什麼可能出錯?

+0

TIMESTAMPDIFF()需要日期時間而不是時間值,而TIME()返回一個字符串,而不是時間。嘗試使用TIMEDIFF()函數,並將其用於字段本身,而不是TIME()函數結果 – 2015-04-02 03:44:20

+0

它完美的工作!謝謝。 – Razeth 2015-04-02 03:53:47

+0

太棒了。我只是張貼它作爲一個答案,如果你可以打擾接受它:) – 2015-04-02 04:40:46

回答

0

TIMESTAMPDIFF()需要一個日期時間,而不是時間值,TIME()返回一個字符串,而不是時間。嘗試使用TIMEDIFF()函數,並將其用於字段本身,而不是TIME()函數結果