0

We have two tables in our database: shifts and workhours. As can be seen, the shifts table reflects that the employee can have more shifts per day. The workhours table just stores the total minutes worked for a given day plus it also stores the number of minutes worked overtime (if there is some ovetime). Overtime is saved when the total minutes is above 480.

Shifts:

id      employee_id     date          minutes_worked
123     12345           2022-04-11    300
456     12345           2022-04-11    200    

Workhours:

id      employee_id     date          minutes_worked      overtime
222     12345           2022-04-11    500                 20

The overtime column is being newly added to the database so for all the data from the past it now has NULL. I need to update this overtime column for all the already saved records. For that I need to count the minutes sum in the shifts table and update the overtime column in such a way that it's linked through the employee_id as well as the date. Can it be done in MariaDB? I looked at similar questions here and tried the following but it didn't work. I'm using MariaDB 10.1.48

UPDATE workhours 
SET 
    overtime = (SELECT 
            SUM(minutes_worked)
        FROM
            shifts
        WHERE
            employee_id = (SELECT 
                    employee_id
                FROM
                    workhours)
                AND date = (SELECT 
                    date
                FROM
                    workhours));

#1093 - Table 'workhours' is specified twice, both as a target for 'UPDATE' and as a separate source for data

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828

0 Answers0