有4个表
bbs_board      bbs_thread      bbs_topic     bbs_count-----------------------------------------------
bbs_board 结构id(论坛ID)     boardname(论坛名)  ...
 1              论坛1
-------------------------------bbs_thread 结构threadid(主题ID)     boardid(论坛ID)  ...
   3                    1
-------------------------------bbs_topic 结构topicid(帖子ID)     threadid(主题ID)  ...
   5                   3
-------------------------------bbs_count 结构total           id        boardname
(统计结果)   (论坛id)     (论坛名)
-----------------------------------------------对应关系   bbs_board.id = bbs_thread.boardid
           bbs_thread.threadid = bbs_topic.threadid
---------------------------------------------------------现在要分别统计不同论坛的帖子数并插入到bbs_count表,就是统计bbs_topic.topicid
结果为bbs_counttotal           id        boardname
 100             1          论坛1
 200             2          论坛2
 150             3          论坛3

解决方案 »

  1.   

    insert into bbs_count(total,id,boardname)
    select count(*) as bbs_count,a.id,a.boardname
    from bbs_board a 
        inner join bbs_thread b on a.id = b.boardid
        inner join bbs_topic c b.threadid = c.threadid
    group by a.id,a.boardname
    order by a.id,a.boardname
      

  2.   

    只执行里面的select抱错
    消息 102,级别 15,状态 1,第 4 行
    'b' 附近有语法错误。
      

  3.   

    --sorry,少了个on insert into bbs_count(total,id,boardname)
    select count(*) as bbs_count,a.id,a.boardname
    from bbs_board a 
        inner join bbs_thread b on a.id = b.boardid
        inner join bbs_topic c on b.threadid = c.threadid
    group by a.id,a.boardname
    order by a.id,a.boardname
    /*
    create table #bbs_board (id int,boardname varchar(100))
    create table #bbs_thread(threadid int,boardid int)
    create table #bbs_topic(topicid int,threadid int)
    create table #bbs_count(total int,id int,boardname varchar(100))insert into #bbs_count(total,id,boardname)
    select count(*) as bbs_count,a.id,a.boardname
    from #bbs_board a 
        inner join #bbs_thread b on a.id = b.boardid
        inner join #bbs_topic c on b.threadid = c.threadid
    group by a.id,a.boardname
    order by a.id,a.boardname
    drop table #bbs_board,#bbs_thread,#bbs_topic,#bbs_count*/