SELECT
COUNT(listings.id), offices.id
FROM offices LEFT JOIN listings ON (offices.id = listings.office_id)
WHERE
listings.status = 1
However, this only returned office/count pair where the count was greater than 0. It seemed like the WHERE condition wasn't being applied correctly, like it was being applied AFTER the tables were joined. I wanted the WHERE condition to be applied to the listings table and then have the results joined to the offices table. So, here's the solution I found: in situations like this, the WHERE clause needed to be added to the ON clause in the LEFT JOIN like this:
SELECT
COUNT(listings.id), offices.id
FROM offices LEFT JOIN listings ON (offices.id = listings.office_id AND listings.status = 1)
Works like a charm.
4 comments:
PERFECT!!
I was just having the same problem!
Now i can count and still show the categories, even if they are empty!
Thanks A MILLION!
Danny
Thanks a lot! Had the same problem!
the trick is done simply by adding "ORDER BY ..." (:
Damnit, y didnt i figured it out by myself lol ty
Post a Comment