Domanda

So I have the program saving work start and end times into a database like so:

  id    worker_id                  start                     end
1351            7    2013-06-26 08:00:00     2013-06-26 17:00:00

It saves te times for each day separately, and what I need to do is get the working hours for a worker in a period of time set in a form, for example one month.

Any ideas?

È stato utile?

Soluzione

Look at SUM() + TIMESTAMPDIFF() functions:

SELECT
    `worker_id`, SUM(TIMESTAMPDIFF(HOUR, `start`, `end`)) as `total`
FROM
    `table`
WHERE
    (`start` BETWEEN 'period-start-date' AND 'period-end-date')
    AND
    (`end` BETWEEN 'period-start-date' AND 'period-end-date')
GROUP BY
    `worker_id`;

Consider adding WHERE clause with constraining dates.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top