如何在NOT IN语句中使用多字段唯一键?

时间:2011-07-12 20:26:43

标签: sql unique

我有一个唯一的密钥,它将通过NOT IN语句运行。你是如何做到这一点的?

SELECT DROPPER_ID, BEGIN_DT, END_DT 
FROM VACATION_TEST 
    WHERE DROPPER_ID and BEGIN_DT and END_DT NOT IN (
    SELECT DROPPER_ID, BEGIN_DT, END_DT 
    FROM VACATION_TEST
        MINUS 
    SELECT REPORTER_VACATION.REPORTER, REPORTER_VACATION.BEGIN_DT, REPORTER_VACATION.END_DT 
    FROM REPORTER_VACATION, DROPPER 
    WHERE DROPPER.REPORTER = REPORTER_VACATION.REPORTER AND PROJECT_CD = 'INTL' );

3 个答案:

答案 0 :(得分:0)

使用where not exists (select * from ... where a=.. and b=..)

答案 1 :(得分:0)

除了NOT Exists,你也可以LEFT JOIN/ IS NULL这假设所有加入字段都是非空的

SELECT vt.DROPPER_ID, vt.BEGIN_DT, vt.END_DT 
FROM VACATION_TEST  vt
LEFT JOIN  (
    SELECT DROPPER_ID, BEGIN_DT, END_DT 
    FROM VACATION_TEST
        MINUS 
    SELECT REPORTER_VACATION.REPORTER, REPORTER_VACATION.BEGIN_DT, REPORTER_VACATION.END_DT 
    FROM REPORTER_VACATION, DROPPER 
    WHERE DROPPER.REPORTER = REPORTER_VACATION.REPORTER AND PROJECT_CD = 'INTL' ) ni
ON vt.DROPPER_ID = ni.DROPPER_ID 
   AND vt.BEGIN_DT =ni.BEGIN_DT
   AND vt.END_DT  = ni.END_DT
WHERE ni.DROPPER_ID is null

答案 2 :(得分:0)

在oracle中,您可以执行类似

的操作
SELECT whatever
FROM table1
WHERE  ( col1, col2 ) NOT IN
       ( SELECT expr1, expr2
         FROM   table2
       )
/

虽然上述一些解决方案可能更有效。我通常会避免使用子查询INNOT IN。但这一切都取决于您的情况和数据模型。