我试图在过去7天内将属于同一项目的用户在表中的所有时间(timers.date_time与Date_time格式)相加,并将未完成任何工作的用户显示为00:00: 00格式。
计时器表
Name Type
timer_id int(11)
user_id int(5)
user_name varchar(250)
Proj_id Int(10)
Proj_name varchar(250
workingOn text
date_time datetime
登录表
Name Type
id_login int(11)
name_login varchar(45)
username varchar(100)
password varchar(18)
useremail varchar(45)
tel_number varchar(12)
type_login int(5)
我的最后一步如下,
SELECT *
FROM
(SELECT timers.user_name,timers.workingOn,Date_Format(timers.date_time,
'%d/%m/%Y') AS DDAte,Time_Format(timers.date_time, '%H:%i:%s') AS Duration
FROM timers
WHERE DATE(timers.date_time) > (NOW() - INTERVAL 7 DAY) AND
Proj_name= 'Project 1'
ORDER BY timer_id DESC) AS x
GROUP BY user_name
以上查询并未对timers.date_time列进行求和 并且缺少在过去7天内没有工作的用户(作为用户名和00:00:00)
我一直在尝试过去4天没有运气,任何帮助都会非常感激。
答案 0 :(得分:0)
您可以使用time_to_sec转换时间(以秒为单位)和sum(sec_to_time)以获取有效时间结果
此示例组中的由x.user_name,x.workingOn,x.DDAte
组成 SELECT x.user_name, x.workingOn, x.DDAte, SEC_TO_TIME(sum( x.duration))
FROM
( SELECT timers.user_name,timers.workingOn,Date_Format(timers.date_time,
'%d/%m/%Y') AS DDAte, TIME_TO_SEC( timers.date_time ) AS Duration
FROM timers
WHERE DATE(timers.date_time) > (NOW() - INTERVAL 7 DAY) AND
Proj_name= 'Project 1'
ORDER BY timer_id DESC) AS x
GROUP BY user_name, x.workingOn, x.DDAte
答案 1 :(得分:0)
如果我认为“日期/时间”确实是“日期/持续时间”,那么这里有一种方法:
SELECT t.user_name, t.workingOn,
COALESCE(SEC_TO_TIME(seconds), '0:00:00')
FROM (SELECT t.user_name, t.workingOn,
SUM(CASE WHEN t.date_time >= curdate() -
interval 7 day)
THEN time_to_sec(time(date_time))
END) as seconds
FROM timers t
WHERE Proj_name = 'Project 1'
GROUP BY t.user_name, t.workingOn
) t
你实际上可以在没有子查询的情况下执行此操作,但我认为分解逻辑会使其更容易理解。