Click to See Complete Forum and Search --> : Basic SQL query question
Bantam
February 17th, 2003, 03:04 PM
I need an SQL query that returns all records that are in one table but not in another table. I'm not too sure how to do it, and I'm still new with sql, but here's some very poor pseudo code:
SELECT fldID, fldName, fldAddress
FROM tblOne
WHERE fldID <> (any fldID's in the other table)
Sorry to be such a n00b, as usual...
-Jacob
antares686
February 17th, 2003, 03:29 PM
Well for performance there are many ways to do this. However without knowing which Database type the standard is liks so.
SELECT fldID, fldName, fldAddress
FROM tblOne
WHERE fldID NOT IN (SELECT fldID FROM tblTwo)
However if using SQL Server or Access it is better to do
SELECT fldID, fldName, fldAddress
FROM tblOne
LEFT JOIN
tblTwo
ON
tblOne.fldID = tblTwo.fldID
WHERE
tblTwo.fldID IS NULL
Most other DBs support the previous which takes better advantage of sets and indexes than the other. Some however may not recognize the LEFT JOIN...ON statement in older versions.
Bantam
February 18th, 2003, 10:13 AM
That worked great, thanks!!
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.