Friday, February 17, 2012

differenct between 2 select statments

I have 2 table (table1 & table2) where both contain documentpackageid column. In the first table I have a select:

SELECT documentpackageid FROM table1 WHERE xxx=94

The second table:

SELECT documentpackageid FROM table2 WHERE yyy<>10

Now I want the different between that 2 select statement. So that I have all documentpackageid from the first select - documentpakcageid from the second select

I have tried with:
SELECT documentpackageid FROM (SELECT documentpackageid FROM table1 WHERE xxx=94) AS foo WHERE foo.documentpackageoid !IN (SELECT documentpackageid FROM table2 WHERE yyy<>10);On Oracle the answer is:
SELECT documentpackageid FROM table1 WHERE xxx=94
MINUS
SELECT documentpackageid FROM table2 WHERE yyy<>10
Some DBMSs have "EXCEPT" instead of "MINUS".

If your DBMS has neither you can do this:

SELECT documentpackageid FROM table1 WHERE xxx=94
AND NOT EXISTS
( SELECT null FROM table2 WHERE yyy<>10
AND table2.documentpackageid = table2.documentpackageid)|||Hi, try this

select t1.documentpackageid -t2.documentpackageid from table1 t1,
table2 t2 where t1.xxx=94 and t2.yyy<>10

Madhivanan|||tony, mysql has neither EXCEPT nor MINUS

and versions prior to 4.1 don't even have subselects!!

here is another solution to this problem --select table1.documentpackageid
from table1
left outer
join table2
on table1.documentpackageid
= table2.documentpackageid
and table2.yyy <> 10
where table1.xxx = 94
and table2.documentpackageid is null

No comments:

Post a Comment