[英]Help with a SQL MINUS statement
我有 3 张桌子,Reporter_Vacation、Dropper、Vacation_Temp。
Reporter_Vacation 有 Reporter、Start_dt、End_dt(3 个组合是唯一的)
Dropper 有 Dropper_ID 和 Reporter(都是唯一的)
Vacation_Temp 具有 Dropper_ID、Start_dt、End_dt(这 3 个组合是唯一的)
如何找到在 Vacation_Temp 中没有在 Reporter_Vacation 中的 Reporter、Start_dt 和 End_dt?
SELECT DROPPER_ID, BEGIN_DT, END_DT
FROM VACATION_TEST
MINUS
SELECT DROPPER.DROPPER_ID, REPORTER_VACATION.BEGIN_DT, REPORTER_VACATION.END_DT
FROM REPORTER_VACATION, DROPPER
WHERE DROPPER.REPORTER = REPORTER_VACATION.REPORTER;
另一种比 MINUS 更标准化和更可索引(因此通常更快)的替代方法是空连接。
使用 LEFT JOIN 引入您不想要的表,将 NULL 留在表中没有值的位置。 然后在 WHERE 子句中测试 NULL。 将其扩展到两个连接表:
SELECT vacation_test.dropper_id, vacation_test.begin_dt, vacation_test.end_dt
FROM vacation_test
LEFT JOIN dropper ON
dropper.dropper_id=vacation_test.dropper_id
LEFT JOIN reporter_vacation ON
reporter_vacation.dropper_id=dropper.dropper_id AND
reporter_vacation.begin_dt=vacation_test.begin_dt AND
reporter_vacation.end_dt=vacation_test.end_dt
WHERE reporter_vacation.begin_dt IS NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.