我有两个表,一个表称为出勤率,另一个表名为Timeslices,我试图获取当前一周的总从Timeslices中减去的出勤时间 Doctrine
。我必须得到这些行,但是我必须对查询中的每一行进行求和和减去,但是我需要学习在一个查询中做到这一点。
这是出勤表的结构:
SELECT * FROM attendance;
+----+---------+---------------------+--------+---------------------+---------------------+
| id | user_id | day | status | check_in | check_out |
+----+---------+---------------------+--------+---------------------+---------------------+
| 1 | 1 | 2019-12-18 00:00:00 | end | 2019-12-18 09:52:00 | 2019-12-18 23:37:02 |
| 2 | 1 | 2019-12-19 00:00:00 | end | 2019-12-19 12:12:00 | 2019-12-19 21:05:00 |
+----+---------+---------------------+--------+---------------------+---------------------+
Timeslice表格:
SELECT * FROM timeslice;
+----+---------------+-------------------------------------+---------------------+---------------------+---------------------+
| id | attendance_id | title | day | start_at | stopped_at |
+----+---------------+-------------------------------------+---------------------+---------------------+---------------------+
| 20 | 1 | Sacar al perro, ducharme y vestirme | 2019-12-18 00:00:00 | 2019-12-18 15:57:50 | 2019-12-18 12:15:36 |
| 21 | 1 | Dormir | 2019-12-18 00:00:00 | 2019-12-18 18:44:30 | 2019-12-18 16:16:44 |
| 22 | 1 | Descansar | 2019-12-18 00:00:00 | 2019-12-18 23:04:53 | 2019-12-18 20:56:29 |
| 23 | 2 | Comer | 2019-12-19 00:00:00 | 2019-12-19 16:03:00 | 2019-12-19 15:37:00 |
| 24 | 2 | Comer | 2019-12-19 00:00:00 | 2019-12-19 16:55:00 | 2019-12-19 16:17:00 |
| 25 | 2 | ducharme | 2019-12-19 00:00:00 | 2019-12-19 19:58:00 | 2019-12-19 17:20:00 |
+----+---------------+-------------------------------------+---------------------+---------------------+---------------------+
这是我当前的query,在其中获得结果,但是随后我必须从查询中计算出以获得所需的结果和SQLFiddle
:http://sqlfiddle.com/#!9/646be/3
SELECT SUM(TIME_TO_SEC(TIMEDIFF(a.check_out, a.check_in))) AS secondsAttendance
, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(t.start_at, t.stopped_at)))
FROM timeslice t
WHERE t.attendance_id = a.id
) secondsPauses
FROM attendance a
GROUP
BY a.id
我之前说过的需要能够在同一查询中完成它,而不必使用PHP和Doctrine
INSERT INTO `timeslice` (`id`, `attendance_id`, `title`, `day`, `start_at`, `stopped_at`)
VALUES(23, 2, 'Comer', '2019-12-19 00:00:00', '2019-12-19 16:03:00', '2019-12-19 15:37:00'),
假设这只是一个错误,也许您正在寻找类似的东西?
SELECT a.id, SUM(TIME_TO_SEC(TIMEDIFF(a.check_out, a.check_in))) AS secondsAttendance, SUM(TIME_TO_SEC(TIMEDIFF(t.start_at, t.stopped_at))) AS secondsPauses, SUM(TIME_TO_SEC(TIMEDIFF(a.check_out, a.check_in))) - SUM(TIME_TO_SEC(TIMEDIFF(t.start_at, t.stopped_at))) AS actualSecondaAttendace -- Get your actual value FROM attendance a LEFT JOIN timeslice t ON t.attendance_id = a.id WHERE yearweek(DATE(a.check_in), 1) = yearweek(curdate(), 1) -- filter by current week (assuming that checkin and checkout is on same day) GROUP BY a.id
这是我从您的数据中得到的结果:
+----+-------------------+---------------------+------------------------+ | id | secondsAttendance | secondsPauses | actualSecondaAttendace | +----+-------------------+---------------------+------------------------+ | 1 | 148506 | 29904 | 118602 | +----+-------------------+---------------------+------------------------+ | 2 | 95940 | 13320 | 82620 | +----+-------------------+---------------------+------------------------+
或者如果您只想为当前一周估值:
SELECT SUM(TIME_TO_SEC(TIMEDIFF(a.check_out, a.check_in))) - SUM(TIME_TO_SEC(TIMEDIFF(t.start_at, t.stopped_at))) AS actualSecondaAttendace FROM attendance a LEFT JOIN timeslice t ON t.attendance_id = a.id WHERE yearweek(DATE(a.check_in), 1) = yearweek(curdate(), 1)
这是我从您的数据中得到的结果:
+------------------------+ | actualSecondaAttendace | +------------------------+ | 201222 | +------------------------+