我有一个MySQL表,记录员工进出时间。
SELECT
device_id as d_id,
date(run_start) as start_date,
sum(elapsed_min)/60.0 as hrs_on,
24 - sum(elapsed_min)/60.0 as hrs_off
FROM
(SELECT *, TIMESTAMPDIFF(minute, run_start, run_end) elapsed_min
FROM
(SELECT t1.device_id, t1.punch as run_start,
(SELECT MIN(punch)
FROM emp_punch_in_out t2
LEFT JOIN emp_map e ON e.device_id = t2.device_id
WHERE t2.device_id = t1.device_id
AND t2.punch > t1.punch
AND t2.stat_log = 'out') as run_end
FROM emp_punch_in_out t1
WHERE t1.device_id = 26 AND t1.stat_log = 'in') t
) tt
GROUP BY device_id, start_date
ORDER BY device_id, start_date
是否可以使用单个查询从用户工作的总小时数中检索总小时数和总小时数?
我使用了以下查询:
#ID device_id Date In_hours Out_hours
1 26 2016-11-23 8.5333 15.4667
从这个查询我得到的结果如下:
#ID device_id Date In_hours Out_hours
1 26 2016-11-23 8.5333 0.30
但我希望结果总计小时数和总工作小时数,包括休息时间。
所需输出
<form id="my_form" method="POST" action="sendmail.php" >
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input id="my_button" type="button" name="send" value="SEND">
</form>
答案 0 :(得分:2)
假设您的输入表名为Table1
:
Select device_id,
Date(punch) As "Date",
Sum(Case When stat_log = 'out' Then TimeStampDiff(minute, punch, next_punch) Else 0 End)/60 As out_hours,
Sum(Case When stat_log = 'in' Then TimeStampDiff(minute, punch, next_punch) Else 0 End)/60 As in_hours
From (
Select id, device_id, punch, stat_log,
(Select punch
From Table1 As n
Where n.device_id = c.device_id
And n.punch > c.punch
And Date(n.punch) = Date(c.punch)
Order By punch asc
Limit 1) As next_punch
From Table1 As c) As x
Group By device_id, Date(punch);
修改强>
将And Date(n.punch) = Date(c.punch)
添加到连接谓词,以便这可以工作多天。