Help with SQL query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brixton
    New Member
    • Nov 2006
    • 35

    #1

    Help with SQL query

    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
    Recognized Expert Contributor
    • Jul 2006
    • 802

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

    [code=mysql]SELECT OWNER.id, COUNT(*) product_count FROM OWNER
    LEFT JOIN PRODUCT ON OWNER.id = PRODUCT.owner_i d
    ORDER BY product_count DESC[/code]

    Comment

    Working...