The logins of each user are stored in the _t_user table of the mysql database 5.5.46 version
+------------+-----------------------+-----------+---------+-----------+
| IP_ADDRESS | Date_Time_from_System | User_name | Event | Effective |
+------------+-----------------------+-----------+---------+-----------+
| XX1030729 | 2022-08-11 15:16:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 15:16:28 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:24:50 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:23:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:23:26 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:22:56 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:22:54 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:20:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:19:40 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:15:06 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:14:12 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:14:00 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:13:30 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 12:22:06 | CIRICILLS | OUT | NULL |
| XX1030729 | 2022-08-11 12:21:04 | CIRICILLS | IN | NULL |
+------------+-----------------------+-----------+---------+-----------+
Each user has three minutes to logout manually.
Each single logout is stored in the same table _t_user.
I need to update on the table _t_user the column Effective with value 'N' when the user log out manually within the expected three minutes.
Example for user_name CIRICILLS
+------------+-----------------------+-----------+---------+-----------+
| IP_ADDRESS | Date_Time_from_System | User_name | Event | Effective |
+------------+-----------------------+-----------+---------+-----------+
| XX1030729 | 2022-08-11 15:16:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 15:16:28 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:24:50 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:23:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:23:26 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:22:56 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:22:54 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:20:52 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:19:40 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:15:06 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:14:12 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:14:00 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 13:13:30 | CIRICILLS | IN | NULL |
| XX1030729 | 2022-08-11 12:22:06 | CIRICILLS | OUT | N |
| XX1030729 | 2022-08-11 12:21:04 | CIRICILLS | IN | N |
+------------+-----------------------+-----------+---------+-----------+
Because the time difference from first login and next manually logout for user_name CIRICILLS, is less than the three minutes.
SELECT TIMEDIFF('2022-08-11 12:22:06','2022-08-11 12:21:04');
+-------------------------------------------------------+
| TIMEDIFF('2022-08-11 12:22:06','2022-08-11 12:21:04') |
+-------------------------------------------------------+
| 00:01:02 |
+-------------------------------------------------------+
1 row in set
Any suggestion?
This is done using MySQL5.7 . Assuming every OUT has an IN before it (no consecutive OUT) , we can use an OUTTER SELF JOIN to form a derived table in which the table1 has a datetime that is right after table2's datetime (except the very first table1's datetime). To ensure the datetime discrepancy between the two tables' datetime is immediate, I used the @row_id trick to generate row_id in chronological order. The rest is just to calculate the
effectivevalue viaCASE. Here is the one I wrote and tested in workbench. Note I took the liberty of adding 4 rows in addition to your original 15 rows for more thorough testing. As commented after each statement which contains an OUT, 3 out of the 4 OUT are qualified as being 'N'.Here is the query: