我有一个sql,可在员工合同到期60天前将记录插入通知表中。
INSERT INTO notification (name)
SELECT employee.employee_name
from EMPLOYEE, employee_info
where EMPLOYEE_INFO.EMPLOYEE_NAME(+) =EMPLOYEE.EMPLOYEE_NAME
AND (LEAVE_TYPE ='Vacation'
or LEAVE_TYPE = 'Personal')
group by EMPLOYEE.EMPLOYEE_NAME, EMPLOYEE.CONTRACT_END_PERIOD, EMPLOYEE.TOTAL_VACATION_LEAVE, EMPLOYEE_INFO.LEAVE_TYPE
HAVING sum(days_taken) > 0
and TO_DATE(contract_end_period,'DD-MM-YYYY') < TO_DATE (SYSDATE,'DD-MM-YYYY') + 60
);
但是,当新员工合同到期时,我只希望将新记录插入到通知表中。
我现在得到的是插入新旧条目(因为我使用的是页面加载sql),这导致表中出现重复项。
答案 0 :(得分:0)
INSERT INTO notification (name)
SELECT employee.employee_name
from EMPLOYEE, employee_info
where EMPLOYEE_INFO.EMPLOYEE_NAME(+) =EMPLOYEE.EMPLOYEE_NAME
AND (LEAVE_TYPE ='Vacation' or LEAVE_TYPE = 'Personal')
-> AND employee.employee_name NOT IN (select notification.name from notification) <-
group by EMPLOYEE.EMPLOYEE_NAME, EMPLOYEE.CONTRACT_END_PERIOD, EMPLOYEE.TOTAL_VACATION_LEAVE, EMPLOYEE_INFO.LEAVE_TYPE
HAVING sum(days_taken) > 0
and TO_DATE(contract_end_period,'DD-MM-YYYY') < TO_DATE (SYSDATE,'DD-MM-YYYY') + 60
);
您需要将查询限制为该表中尚不存在的人。上面给出了一个示例:“-> AND employee.employee_name NOT IN(从通知中选择notification.name)<-” 标记。