开发者

How do you use a multiple field unique key in a NOT IN statement?

开发者 https://www.devze.com 2023-03-19 14:50 出处:网络
I have a unique key that is going to be run through a NOT IN statement.How do you accomplish this? SELECT DROPPER_ID, BEGIN_DT, END_DT

I have a unique key that is going to be run through a NOT IN statement. How do you accomplish this?

SELECT DROPPER_ID, BEGIN_DT, END_DT 
FROM VACATION_TEST 
    WHERE DROPPER_ID and BEGIN_DT and END_DT NOT IN (
    SELECT DROPPER_I开发者_开发百科D, 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' );


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


Aside from NOT Exists you can also you LEFT JOIN/ IS NULL this assumes that all the joining fields are NOT 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


In oracle you can do something like

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

Although some of the above solutions are probably more efficent. I usually avoid IN and NOT IN with subqueries. But it all depends on your situation and data model.

0

精彩评论

暂无评论...
验证码 换一张
取 消