select
    a.name,
    b.title,
    renote = (select isnull(count(*),0) from answer where origid = b.id)
from
    people a,
    note   b
where
    a.id = b.writerid

解决方案 »

  1.   

    select B.name as '发帖人'
           ,A.title as '标题'
           ,C.num as '回复数'
    from note A
    join people B on A.writerid=B.id
    left join (
                 select origid,count(1) as 'num'
                 from answer
                 group by origid
              )C on C.origid=A.id
      

  2.   

    select B.name as '发帖人'
           ,A.title as '标题'
           ,isnull(C.num,0) as '回复数'
    from note A
    join people B on A.writerid=B.id
    left join (
                 select origid,count(1) as 'num'
                 from answer
                 group by origid
              )C on C.origid=A.id
      

  3.   

    --生成测试数据
    create table people(id int,[name] varchar(20))
    insert into people select 1,'buman'
    insert into people select 2,'regretwang2000'
    insert into people select 3,'sun'
    insert into people select 4,'sunwang81081'create table note(id int,title varchar(20),content varchar(20),writerid int)
    insert into note select 1,'shsh','shshsh',1
    insert into note select 2,'sss' ,'ssss'  ,1
    insert into note select 3,'dddd','ddddd' ,2
    insert into note select 4,'ddd' ,'dd'    ,1create table answer(id int,answercontent varchar(20),origid int)
    insert into answer select 1,'sssssssssss',1
    insert into answer select 2,'sss',1
    insert into answer select 3,'wwwwwwwwwww',1
    --执行查询
    select
        a.name,
        b.title,
        renote = (select isnull(count(*),0) from answer where origid = b.id)
    from
        people a,
        note   b
    where
        a.id = b.writerid
    --输出结果
    /*
    name            title   renote
    --------------  ------  -------
    buman           shsh    3
    buman           sss     0
    regretwang2000  dddd    0
    buman           ddd     0
    */