Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with SQL query

Newbie
 
Join Date: Nov 2006
Posts: 17
#1: May 4 '09
Hello!

I have a DB setup which simplified looks like this:

PRODUCT(id, owner_id)
OWNER(id)

What I want to do is to produce a list of how many products each owner has, and then sort it in some way, all in a single query.

How do I go about this?

Thanks!

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: May 4 '09

re: Help with SQL query


Use the LEFT JOIN to produce the results e.g.

Expand|Select|Wrap|Line Numbers
  1. SELECT OWNER.id, COUNT(*) product_count FROM OWNER 
  2. LEFT JOIN PRODUCT ON OWNER.id = PRODUCT.owner_id
  3. ORDER BY product_count DESC
Reply


Similar MySQL Database bytes