Help - Counting problem using INNER JOIN

  • SELECT COUNT(table1.ID1) AS counter, table2.ID2, table2.ID3 FROM

    table2 INNER JOIN table1

    ON

    table2.ID2 = table1.ID2

    AND

    table2.ID3 = table1.ID3

    GROUP BY table2.ID2, table2.ID3

    Obviously this only returns stuff that is exists in Table1. I need a way to do the same that will return zero where there's no entry in table one yet. Any idea?

  • Try right join

  • SELECT SUM(CASE table1.ID1 WHEN NULL THEN 1 ELSE 0 END) AS counter, table2.ID2, table2.ID3 FROM

    table2 LEFT JOIN table1

    ON

    table2.ID2 = table1.ID2

    AND

    table2.ID3 = table1.ID3

    GROUP BY table2.ID2, table2.ID3

  • Thanks David, you steered me in the right direction - got it working correctly using 'LEFT JOIN'

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply