View Full Version : COUNT returns 1 instead of 0


Kmerker
04-13-2002, 06:00 AM
I have 2 tables. PARTS and VENDORS. I want to run a query that returns all the VENDORS and a COUNT of how many PARTS they provide. Some Vendors provide 0 parts but my query returns the value '1' for these vendors. Whats Wrong?

SELECT VENDOR.ID, VENDOR.NAME, Count(*)
FROM VENDOR LEFT JOIN PARTS ON VENDOR.ID=PARTS.VENDOR
GROUP BY VENDOR.ID,VENDOR.NAME;

RV
04-13-2002, 09:15 AM
Use an INNER JOIN instead of an LEFT JOIN.

If you're using a LEFT JOIN, all records from your left table VENDOR will be selected, even if there no related records in your right table (PARTS).
In other words, if there is no related record in your PARTS table found for a Vendor, a Null value is added, so your Count results in 1 (one record found).

By using an INNER JOIN, only those records are selected which meet the join condition.

Greetings,

RV