SELECT li.listing_id, li.title, p.name, c.comment, COUNT( * ) AS pic_count
FROM listings li
LEFT JOIN photos p ON li.listing_id = p.listing_id
LEFT JOIN comments c ON li.listing_id = c.listing_id
WHERE li.listing_id =1
GROUP BY li.listing_id
LIMIT 1;
above query gives me the count of photos for given listing_id, same way i want the count of comments for that listing_id in same query, is that possible ? 2 counts in one query ..
Your count is wrong. p.name should not be in the field list if you want to group by listing. But even without it, you get 1 for listings without a photo, because of the
*in count.If your foto’s got a unique id, you can do this: