Computing.Net > Forums > Database > SQL Select statement help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SQL Select statement help

Reply to Message Icon

Name: mking (by mkingrey)
Date: April 12, 2006 at 14:23:02 Pacific
OS: xp
CPU/Ram: n/a
Product: n/a
Comment:

I have Table1 with FieldA and Table2 with FieldA. I would like to query Table1 for all records that DO NOT exist in FieldA of Table2. I have tried the following but it doesn't filter correctly. Any suggestions?? Thx!!

SELECT Table1.FieldA FROM Table1, Table2 WHERE Table1.FieldA <> Table2.Field



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: April 12, 2006 at 15:24:11 Pacific
Reply:

I'm not real good with SQL, but I think the solution would be to join the two tables (inner or outer?) such that every record in tableA would have it's corresponding value from B as well. Then put in your where clause that you only want records where the B value is null.

This probably isn't right, but it should be the right direction:

SELECT Table1.FieldA as field1, Table2.FieldA as field2
FROM Table1, Table2
WHERE Table1.FieldA=Table2.FieldA
AND field2 IS NULL


Michael J


0

Response Number 2
Name: mdow
Date: April 13, 2006 at 06:27:32 Pacific
Reply:

SELECT TABLE1.ID, TABLE2.ID
FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
WHERE (((TABLE2.ID) Is Null));


0

Response Number 3
Name: babaji
Date: April 15, 2006 at 10:41:54 Pacific
Reply:

Try this

SELECT Table1.FieldA FROM Table1 WHERE Table1.FieldA NOT in ( select Table2.FieldA from Table2 )


0

Response Number 4
Name: SN
Date: April 16, 2006 at 11:23:32 Pacific
Reply:

Both mdow and babaji have correct solutions (And Michael, except the join should be a left join as mdow suggested) , but mdow's will be much, much faster. You should avoid having subqueries in the WHERE statement whenever possible.

Good luck,
-SN


0

Response Number 5
Name: Michael J (by mjdamato)
Date: April 16, 2006 at 13:15:11 Pacific
Reply:

As I said, I didn't think my structure was quite right, but I was pretty sure of the logic behind it.

Michael J


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: SQL Select statement help

select statement help! www.computing.net/answers/dbase/select-statement-help/283.html

Need Urgent Help in MS SQL Express www.computing.net/answers/dbase/need-urgent-help-in-ms-sql-express-/384.html

select only one row www.computing.net/answers/dbase/select-only-one-row/206.html