Default count(*) to 0

Discussion in 'Databases' started by sc_king, Dec 27, 2009.

  1. #1
    When left joining a count(*) to a each id, is there a way to default the count(*) to 0 instead of NULL if there are none?
     
    sc_king, Dec 27, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Can you post the query you are trying to run?
     
    jestep, Dec 28, 2009 IP
  3. sc_king

    sc_king Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    select * from companies LEFT JOIN (select company_id, count(*) from parts group by company_id) as parts_per_company ON companies.id = parts_per_company.company_id

    if a company does not have any parts, I want it to display 0 instead of null on the left join.
     
    sc_king, Dec 28, 2009 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    Use
    COUNT()
    Code (markup):
    instead of
    COUNT(*)
    Code (markup):
     
    digitalpoint, Dec 28, 2009 IP