SELECT 
    top 2 t1.*,isnull(t2.[count],0) as [count] 
FROM 
    Article t1 
LEFT JOIN 
     (SELECT COUNT(C_Id) as [count],A_Id FROM Comment GROUP By A_Id)  t2
ON 
    t1.A_Id=t2.A_Id

解决方案 »

  1.   

    select top 2 a.a_id , count(b.c_id) from Article a left join Comment b on a.a_id = b.a_id
      

  2.   


       使用查询达到了要求
    但是我在使用存储结构的时候 出现了一个错误    错误 21037:[SQL-DMO] 在 Text属性的"CREATE... " 语句中指定的名称必须与Name 属性匹配,
    而且后面必须跟有效的TSQL语句
       这是怎么回事了!
      

  3.   

    删掉原来的存储过程,在查询分析器里执行你建立存储过程的语句CREATE PROC test
    asSELECT 
        top 2 t1.*,isnull(t2.[count],0) as [count] 
    FROM 
        Article t1 
    LEFT JOIN 
         (SELECT COUNT(C_Id) as [count],A_Id FROM Comment GROUP By A_Id)  t2
    ON 
        t1.A_Id=t2.A_Id
      

  4.   

    try:
    select A.*,(select count(*) from Command where A_Id=A.A_Id)
    from Article A