如何在sql中获取记录的数量?

时间:2012-08-27 10:39:43

标签: sql-server-2008 count

大家好我有两张桌子

    Projects
  ----------
  ProjectID|ProjectNAme
    1      |asdasd
    2      |adas
    3      |asdfdf

  Bugs
-------
BugID|Title|ProjectID
  1  |asda |1
  2  |asdfs|1
  3  |sdfsdf|2
  4  |asfsd|2

现在我想要的是,得到项目没有错误的数量...如果项目没有任何错误,那么我应该把计数作为0..i一直在尝试这个查询...如果在Bug表中不存在projectID,我可以得到计数0,任何人都可以帮助我

 Select 
    P.projectName
    , Count(B.BugID) As BugCount  
 from bugs B inner join projects P 
 on P.ProjectId = B.ProjectId
 group by P.projectName

1 个答案:

答案 0 :(得分:2)

 Select P.projectName, Count(B.BugID) As BugCount  
 from projects P 
      left join bugs B 
          on P.ProjectId = B.ProjectId 
 group by P.projectName