I have two tables and want to join them together in such a way that all values in the second table are excluded. When I join both tables, I only want the values in table 1, and don't want the values in both tables or the values in just table 2.
I thought this would be done with a LEFT JOIN or LEFT OUTER JOIN, but I'm finding some odd findings.
When I check all the data, I have the following count.
-- TOTAL LEADS: 10067
SELECT COUNT(*) FROM sold_leads AS sl
WHERE sl.affiliate_id IN(1000,1001,1002,1033)
AND sl.create_date >= '2013-1-1'
AND sl.lead_type = 'AUTO';
However, when I attempt to find only the values just in table 1, I find a higher number, which is just not possible.
-- How many had No suspension/dui, sr22, and <=2 accidents AND <=2 tickets: 13931
SELECT COUNT(*) FROM sold_leads AS sl
INNER JOIN drivers AS dr ON sl.lead_id = dr.lead_id
LEFT OUTER JOIN duis AS duis ON sl.lead_id = duis.driver_id
LEFT OUTER JOIN accidents AS ac ON sl.lead_id = ac.driver_id
WHERE sl.affiliate_id IN(1000,1001,1002,1033)
AND sl.create_date >= '2013-1-1'
AND dr.relationship_type = 'SELF';
There's just no way that a total count would be less than the count where I left join some tables and add some restrictions. What am I doing wrong.
Also, I know I haven't provided the schema, but how would I select just <=2 accidents. There is an accidents table that has a driver_id, but I've played with different variation of HAVING(COUNT(*)) and had no luck.
Help!?