[英]mysql total working hours
id title start end 1 Doing Coding for this project. 2013-04-02 02:00:00 2013-04-02 04:00:00 2 Doing Coding for this project. 2013-04-02 04:00:00 2013-04-02 06:00:00 3 Doing Coding for this project. 2013-04-02 06:00:00 2013-04-02 06:30:00
I have above MySQL
database table record.我有上面的MySQL
数据库表记录。 Now i want to get the total number of hours.
现在我想获得总hours.
I am developing TimeSheet Management Application
and we need to display total working hours with minutes and second of employee.我正在开发TimeSheet Management Application
,我们需要显示员工的分钟和秒的总工作时间。 (ie 04:30:00 according to data i share) (即根据我分享的数据为 04:30:00)
what i have tried?我试过什么?
SELECT HOUR(TIMEDIFF(end,start)) AS 'totalHour'
but works only for each row not on all records. SELECT HOUR(TIMEDIFF(end,start)) AS 'totalHour'
但仅适用于每一行而不适用于所有记录。
I have also tried TIMESTAMPDIFF
.我也试过TIMESTAMPDIFF
。
Is this possible?这可能吗?
EDIT编辑
From the answer i have received from people i have tried every single of them but everytime i just get 4
or 4.5000
but it should return 06:30:00
.从我从人们那里得到的答案中,我尝试了他们中的每一个,但每次我只得到4
或4.5000
但它应该返回06:30:00
。
The range of HOUR()
function is 0 to 23
so it's not correct to use it for total hours in diff. HOUR()
函数的范围是0 to 23
因此在 diff 中使用它的总小时数是不正确的。
For single value you could use TIMESTAMPDIFF()
like:对于单个值,您可以使用TIMESTAMPDIFF()
例如:
SELECT TIMESTAMPDIFF(HOUR, start, end) AS `totalHour` FROM ...
If you want to calculate it for whole project, you have to sum up all time differences and them print it formatted probably with funciton like TIME_FORMAT()
which prints hour larger than 24:如果你想为整个项目计算它,你必须总结所有的时间差异,然后他们打印它可能使用像TIME_FORMAT()
这样的TIME_FORMAT()
打印大于 24 的小时:
If the time value contains an hour part that is greater than 23, the %H and %k hour format specifiers produce a value larger than the usual range of 0..23.如果时间值包含大于 23 的小时部分,则 %H 和 %k 小时格式说明符生成的值大于通常的 0..23 范围。 The other hour format specifiers produce the hour value modulo 12.其他小时格式说明符产生模 12 的小时值。
So you can use:所以你可以使用:
SELECT TIME_FORMAT( SEC_TO_TIME( SUM( TIME_TO_SEC(end) - TIME_TO_SEC(start))), "%H")
AS `totalHour`
FROM ...
GROUP BY sort_of_project_id
If you need seconds/minutes too (as suggested in comment), use either:如果您也需要秒/分钟(如评论中所建议),请使用:
time_to_sec( <left side of select>)/3600
which will return value like 4.84 hours
time_to_sec( <left side of select>)/3600
将返回类似4.84 hours
值TIME_FORMAT( ..., "%H:%m:%s")
which will display 4:38:24
TIME_FORMAT( ..., "%H:%m:%s")
将显示4:38:24
Try like this, it will give you the total no of hours:像这样尝试,它会给你总小时数:
For Example:例如:
SELECT sum(time_to_sec(timediff(end, start ))/ 3600) AS 'totalHour' from test;
If you run this query for above table you given, it shows the output 4.5
hours.如果您对上面给出的表运行此查询,它会显示4.5
小时的输出。
Hope it will help you.希望它会帮助你。
I already answered in other thread https://stackoverflow.com/questions/44560345/query-is-not-working/44567322#44567322我已经在其他线程中回答了https://stackoverflow.com/questions/44560345/query-is-not-working/44567322#44567322
I just created a temporary table called dataimport我刚刚创建了一个名为 dataimport 的临时表
[Table Format][1] [表格格式][1]
and wrote a query as,并写了一个查询,
SELECT `EnNo`, work_dt,
SEC_TO_TIME(sum(TIMESTAMPDIFF(SECOND,login,logout))) as time_worked
from (
SELECT `EnNo`, date(`DateTime`) as work_dt, `DateTime` as login
, coalesce(
(SELECT MIN(`DateTime`)
FROM `dataimport` as b
WHERE a.EnNo = b.EnNo
and date(a.`DateTime`) = date(b.`DateTime`)
and b.`DateTime` >= a.`DateTime`
and b.`INOUT` = 'E'
), now()) AS logout
FROM `dataimport` AS a
WHERE a.`INOUT` = 'S'
) as t
GROUP BY `EnNo`, work_dt
Finally got the output as,最后得到输出,
[Output][2] [输出][2]
Hope this is what you are lookin on.希望这就是你正在寻找的。 [1]: https://i.stack.imgur.com/OEEMe.png [2]: https://i.stack.imgur.com/g6ivm.png [1]: https : //i.stack.imgur.com/OEEMe.png [2]: https : //i.stack.imgur.com/g6ivm.png
Change your改变你的
SELECT HOUR(TIMEDIFF(end,start)) AS 'totalHour'
to到
SELECT sum(TIMESTAMPDIFF(HOUR, end, start)) AS 'totalHour'
SELECT IF(DATE(datetime_end) = DATE(datetime_start), TIMEDIFF(datetime_end,datetime_start), IF(DATEDIFF(datetime_start,datetime_end) > 1, ADDTIME( TIME_FORMAT(CONCAT((DATEDIFF(datetime_start,datetime_end) - 1) * 8,':00:00'), "%H:%i:%s"), ADDTIME( TIMEDIFF(datetime_end,CONCAT(DATE(datetime_end),' 08:00:00')), TIMEDIFF(CONCAT(DATE(datetime_start),' 17:00:00'),datetime_start) ) ) , ADDTIME( TIMEDIFF(datetime_end,CONCAT(DATE(datetime_end),' 08:00:00')), TIMEDIFF(CONCAT(DATE(datetime_start),' 17:00:00'),datetime_start) ) ) ) AS total_working_hrs FROM table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.