帮助SQL MINUS语句

时间:2011-07-12 21:28:46

标签: sql

我有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;

1 个答案:

答案 0 :(得分:3)

比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