Skip to content
Advertisement

MySQL Attendance IN & OUT columns with correct times

I have a database for attendance, it works fine as long as the person does not work over 2 dates. I want to utilize IN and OUT system for records but I do not know how to do the final step, and what I saw on the forum does not work on MySQL or I am doing something wrong there. This is my database and queries are under. BTW Database is built using PHPmyadmin and MySQL Workbench.

This are the Queries, and last one is how I would want the table look like but with IN and OUT times correct, now are both same.

This are the results from my queries i posted.

and last query is this one, but it has always same time in IN and OUT

Advertisement

Answer

Assuming that:

  1. 1st record for each separate emp_id is IN event
  2. There is no lost events

fiddle


Idea.

We enumerate all rows for each employee separately starting with zero. So first IN is 0, first OUT is 1, 2nd IN is 2 and so on.

You can see that matched IN and OUT events will give the same result after integer divide their numbers by 2. And the reminder for IN will be 0 whereas for OUT it will be 1.

This is enough for correct joining.

Second copy of CTE table is joining using LEFT join because the last IN row may have no according OUT row – this means that the employee is now present at the object. And final row will contain NULL in out_date column in this case.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement