1.emp_ot表用于 定义员工在特定时期内可以做的最大ot hours
2. daily_attend描述实际发生的事情
3.我的表格缩短形式如下
mysql> select punchDate,empNO,ot from daily_attend;
+------------+--------+----------+
| punchDate | empNO | ot |
+------------+--------+----------+
| 2012-02-20 | 000123 | 02:00:00 |
| 2012-02-02 | 000123 | 01:00:00 |
| 2012-02-01 | 000126 | 01:00:00 |
| 2012-02-01 | 000123 | 01:00:00 |
+------------+--------+----------+
4 rows in set (0.01 sec)
mysql> select permitId,permitMonth,empNo,dayFrom,dayTO,permitOt from
emp_ot;
+----------+-------------+--------+------------+------------+----------+
| permitId | permitMonth | empNo | dayFrom | dayTO | permitOt |
+----------+-------------+--------+------------+------------+----------+
| 1 | 02 | 000123 | 2012-02-01 | 2012-02-10 | 02:00:00 |
| 2 | 02 | 000123 | 2012-02-20 | 2012-02-25 | 03:00:00 |
| 3 | 02 | 000126 | 2012-02-01 | 2012-01-10 | 02:00:00 |
| 4 | 03 | 000123 | 2012-03-01 | 2012-03-10 | 05:00:00 |
+----------+-------------+--------+------------+------------+----------+
4 rows in set (0.00 sec)
4.在这里我想更新emp_ot.workedOt以了解员工在每个permitId下工作了多少小时
前任:
+----------+------------+----------+----------+
| permitId | empNo | workedOt | permitOt |
+----------+----------+----------+----------+
| 1 | 000123 | 02:00:00 | 02:00:00 |
| 2 | 000123 | 02:00:00 | 03:00:00 |
| 3 | 000126 | 01:00:00 | 02:00:00 |
| 4 | 000123 | 00:00:00 | 05:00:00 |
+----------+------------+----------+----------+
5.我正在使用 mysql 事件,它在每天 00:00:01 驱动,因为表应该自动更新而不会弄乱旧数据
6.我的尝试如下:
DROP EVENT IF EXISTS make_emp_ot;
DELIMITER |
CREATE EVENT make_emp_ot
ON SCHEDULE EVERY 1 DAY STARTS
TIMESTAMP(CURRENT_DATE,'00:00:01')
DO
BEGIN
INSERT INTO emp_ot( permitMonth,empNo, dayFrom, dayTo, workedOt)
SELECT o.permitId,EXTRACT(MONTH FROM CURRENT_DATE)AS
permitMonth ,o.empNo,'','',(
SELECT ifnull( SEC_TO_TIME(SUM(TIME_TO_SEC(d.ot)))
,'00:00:00')
FROM daily_attend d
WHERE d.punchDate BETWEEN o.dayFrom AND o.dayTo
GROUP BY o.empNo)AS ot
FROM emp_ot o
LEFT JOIN daily_attend d on o.empNo=d.empNo
WHERE o.permitMonth=EXTRACT(MONTH FROM CURRENT_DATE)
ON DUPLICATE KEY UPDATE
workedOt=VALUES(workedOt) ;
END; |
DELIMITER ;
7.我怀疑有坏表加入,实际上我不想插入部分,如何只使用更新部分?
8.请帮我纠正这些事情。谢谢大家。