If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > differenct between 2 select statments

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-04, 05:08
chitech chitech is offline
Registered User
 
Join Date: Oct 2003
Posts: 18
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);
Reply With Quote
  #2 (permalink)  
Old 12-16-04, 05:58
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
On Oracle the answer is:
Code:
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:

Code:
SELECT documentpackageid FROM table1 WHERE xxx=94
AND NOT EXISTS
( SELECT null FROM table2 WHERE yyy<>10
 AND table2.documentpackageid = table2.documentpackageid)
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 12-16-04, 08:33
Madhivanan Madhivanan is offline
Registered User
 
Join Date: Oct 2003
Posts: 357
Hi, try this
Code:
select  t1.documentpackageid -t2.documentpackageid from table1 t1, 
table2 t2  where t1.xxx=94 and t2.yyy<>10
Madhivanan
Reply With Quote
  #4 (permalink)  
Old 12-16-04, 09:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
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 --
Code:
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 12-16-04 at 09:12.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On