不显示删除回复显示所有回复显示星级回复显示得分回复 两表查询,查询公司的时候,顺便把该公司的产品数量列出来产品的数量没有在表里存放,是需要统计的,麻烦各位了公司表 compayID gsmc
1 A
2 B
3 C
4 D产品表 gongying
ID companyID
1 2
2 2
3 4
4 1查询公司得到表
ID gsmc gsum
1  A    1
2  B    2
3  C    0
4  D    1

解决方案 »

  1.   

    select
      row_number(order by getdate()) as id,
      a.id,
      isnull(count(b.companyID),0) as gsum
    from
       compay a left join ongying b
    on
      a.id=b.companyID
    group by
      a.id 
      

  2.   

    select a.id,a.gsmc,
    sum(case when b.companyid is null then 0 else 1 end)as gsum
    from compay a left join gongying on a.id=b.companyid
    group by a.id,a.gsmc
      

  3.   


    select a.*,b.数量 from 公司 a outer apply(select 数量=sum(数量) from 产品表 where 公司Id=a.公司Id) b