Jump to content


help query over two tables with count items... please....

  • Please log in to reply
1 reply to this topic

#1 dfrojd

  • Members
  • PipPip
  • Member
  • 11 posts

Posted 11 September 2006 - 08:22 AM

I have two tables (mysql).

One: "regions" with the fields "region_id" and "region"
Two: "objects" with the fields "ID"; "region_id" and "properties".
Every object belongs to a specific region (linked by region_id).

Now I want a list of all my regions sorted by, and displaying, how many objects belong to every specific region.

Can anyone help me with this one?
Thanks Dennis

#2 fenway

  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 11 September 2006 - 02:07 PM

SELECT r.region, COUNT(o.ID)
FROM regions AS r
LEFT JOIN objects AS o ON ( o.region_id = r.region_id )
GROUP BY r.region_id

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users