MySQL负责按小时定义的中断

时间:2017-08-30 19:55:32

标签: mysql sql

我有两张桌子,一张(个人工作),我记录员工的工作时间,另一张表格,我存储每小时员工的工作时间,具体取决于工作时间。员工可以根据工作时间的不同小时费率。

我需要每位员工每月报告一次,显示所有工作班次的清单以及他的收入。

personal_work

id | work_date  | time_from | time_to  | personal_id
 1 | 2017-01-01 | 16:00:00  | 22:00:00 | 3
 2 | 2017-01-02 | 18:00:00  | 01:00:00 | 3
 3 | 2017-01-03 | 15:00:00  | 02:00:00 | 3
 4 | 2017-01-01 | 20:00:00  | 00:00:00 | 4

work_hour

id | personal_id | valid_date_from | valid_date_to | time_from | time_to  | amount
 1 |           3 | 2013-01-01      | 2017-01-31    | 07:00:00  | 23:00:00 | 5.00
 2 |           3 | 2013-01-01      | 2017-01-31    | 23:00:00  | 07:00:00 | 7.00
 3 |           3 | 2017-02-01      |          null | 07:00:00  | 23:00:00 | 8.00
 4 |           3 | 2017-02-01      |          null | 23:00:00  | 07:00:00 | 9.00
 5 |           4 | 2013-01-01      |          null | 07:00:00  | 23:00:00 | 11.00
 6 |           4 | 2013-01-01      |          null | 23:00:00  | 07:00:00 | 15.00

personal_id = 3

需要的结果
 personal_id | worked_from         | worked_to           | paid
           3 | 2017-01-01 16:00:00 | 2017-02-01 22:00:00 | 30.00 (6x 5.00)
           3 | 2017-01-02 18:00:00 | 2017-02-02 01:00:00 | 39.00 (5x 5.00 + 2x 7.00)
           3 | 2017-01-03 15:00:00 | 2017-02-03 02:00:00 | 61.00 (8x 5.00 + 3x 7.00)

我能够获得工作班次清单,但是没有找到方法来获得正确的工作时间金额,并且如果员工有一个班次,他可以获得两种不同的工资。

SELECT
  work_date,
  time_from,
  time_to
FROM
  personal_work
WHERE work_date BETWEEN '2017-01-01'
  AND '2017-01-31'
  AND personal_id = 3

在MySQL中有没有一种优雅的方法来处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)

So, from your example I can assume that a single personnel has only one record in table personal_work per day. Also they have a fixed work rate per any given day. If that is true, then you can user the following query:

loc