SELECT w.name_worker, w.surname_worker, w.midname_worker, w.id_worker, SUM(number) numb FROM workers w LEFT JOIN exhours e ON (w.id_worker = e.id_worker) LEFT JOIN orders o ON (e.id_order = o.id_order) WHERE o.datetime_order >= '$date_start' AND o.datetime_order <= '$date_end' GROUP BY w.id_worker 

I use this query to calculate the number of hours worked by an employee for a period of time. How can you add to the issue of all employees from the workers table, so that there are those who have not worked for this period? They advised me to use left join , but I haven’t changed anything

    1 answer 1

    If there is a WHERE , then its condition in the query is mandatory, therefore users who have not completed anything probably have no entries in the orders table, so your condition cuts off those.

    You need to make this condition in the JOIN and then there will be employees who have not worked anything in this period.

     SELECT w.name_worker, w.surname_worker, w.midname_worker, w.id_worker, SUM(number) numb FROM workers w LEFT JOIN exhours e ON (w.id_worker = e.id_worker) LEFT JOIN orders o ON (e.id_order = o.id_order AND o.datetime_order >= $date_start AND o.datetime_order <= $date_end) GROUP BY w.id_worker 
    • Thank you, it works. And you can also suggest in a similar moment. I will now create another topic - Ivan