Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Design zur Darstellung des Ein- und Auscheckens von Mitarbeitern

Ich weiß, dass dieser Beitrag veraltet ist, aber dies ist für jemanden, der immer noch nach einer Lösung sucht:

Anwesenheitstabellenstruktur

id              | int
employee_code   | varchar
status          | enum('check_in','check_out')
created         | datetime

Daten

id  employee_code   status          created

1   EMP0001         check_in        2016-08-20 09:30:30
2   EMP0001         check_out       2016-08-20 18:15:00
3   EMP0002         check_in        2016-08-21 14:52:48
4   EMP0002         check_out       2016-08-21 21:09:18

Abfrage

SELECT 
  A1.employee_code,
  A1.created AS check_in_at,
  A2.created AS check_out_at,
  TIMEDIFF(A2.created, A1.created) AS total_time 
FROM
  tbl_attendances AS A1 
  INNER JOIN tbl_attendances AS A2 
    ON A1.employee_code = A2.employee_code 
    AND DATE(A1.created) = DATE(A2.created) 
WHERE 1 = 1 
  AND A1.status = 'check_in' 
  AND A2.status = 'check_out' 
  AND DATE(A1.created) BETWEEN '2016-08-20' 
  AND '2016-08-21' 
  AND DATE(A2.created) BETWEEN '2016-08-20' 
  AND '2016-08-21' 
ORDER BY A1.created DESC

Ergebnisse

employee_code   check_in_at            check_out_at           total_time

EMP0002         2016-08-21 14:52:48    2016-08-21 21:09:18    06:16:30
EMP0001         2016-08-20 09:30:30    2016-08-20 18:15:00    08:44:30

Fügen Sie für einen bestimmten Mitarbeiter AND A1.employee_code = 'EMP0001' hinzu in WHERE Klausel