Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 21st, 2004, 11:50 AM
Clif001 Clif001 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 2 Clif001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Selecting Unmatched Records Based On Multiple Fields

I need to list all the records in Table2 which don't have matching field values in Table1.

This the the exact opposite of what I need:
SELECT DISTINCT
Field1,
Field2,
Field3,
Field4,
Field5
FROM
[Table1]
WHERE EXISTS(
SELECT DISTINCT
FieldA,
FieldB,
FieldC,
FieldD,
FieldE
FROM
[Table2]
)

The above seems to give me all records in Table1 in which the five fields match the five fields specified in Table2. What does not show up is the test record I put in Table2 which is not in Table1.

What I need, however, is the exact opposite.

I tried the above using NOT EXISTS but I get no records at all.

How do do this?

Reply With Quote
  #2  
Old July 22nd, 2004, 12:03 AM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
Thumbs up

SELECT DISTINCT
FieldA,FieldB,FieldC,
FieldD,FieldE
FROM [Table2]

WHERE FieldA NOT IN (SELECT DISTINCT Field1 From [TABLE1]) OR
FieldB NOT IN (SELECT DISTINCT Field2 From [TABLE1]) OR

FieldC NOT IN (SELECT DISTINCT Field3 From [TABLE1]) OR
FieldD NOT IN (SELECT DISTINCT Field4 From [TABLE1]) OR
FieldE NOT IN (SELECT DISTINCT Field5 From [TABLE1])

Regards

Tonny Soeroso

Reply With Quote
  #3  
Old July 22nd, 2004, 10:32 PM
basementjack basementjack is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 basementjack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey Cliff -

I'm at home now, but if memory strikes me, I've seen this done in a much simpler way.

First up - if you have MS ACCESS, use it with an odbc connection and you can use the query tool in there to experiment very easily (note for some reason access ADP projects - that connect directly to the database without using ODBC, for some reason don't use the same query tool) Access has a query wizard that will do what you need, and you can switch to the SQL view and use that code in your ASP page with little modification.


ok I think the query you are looking for is a combination of a join and a where statement that looks for a null value in the other table

this works because specifiying the value from the joined table will either return a value if there's a match, or it will return null, which is what you're looking for.

SELECT Table1.*
FROM Table1 LEFT JOIN table2 ON
(table1.[fielda] = table2.[fielda]
AND
table1.[fielda] = table2.[fielda]
and etc...
)
WHERE (((table2.[fielda]) Is Null));

Reply With Quote
  #4  
Old July 23rd, 2004, 07:17 AM
Clif001 Clif001 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 2 Clif001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
My co-worker decided on another tact.

Thanks for the help, though.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Selecting Unmatched Records Based On Multiple Fields


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT