我用asp.net做论坛,数据库采用sql server 2000 
把主贴和跟帖分别放入两张表里面
怎样将主贴和跟贴联合起来
主贴表
id          帖子编号
uid         用户编号
title       帖子标题
content     帖子内容
counts      帖子点击次数
endtimes    最后更新时间
跟帖表
id          编号
zid         主题编号(对应主贴表里的编号)
uid         回帖人编号
content     回帖内容
fbtimes     回帖时间比如主贴表里有一个主题,跟帖表里有这个主题的两个跟帖,怎样将这两个表的内容联合起来,变成三条数据显示在页面上???

解决方案 »

  1.   

    select ID,... from 主贴表 where 条件unionselect ID,... from 从表  where 条件列要一致,就是类型和数量一致 
      

  2.   

    select id,uid,content from 主贴表 where id=1
    union all
    select zid,uid,content from 跟贴表 where zid=1
      

  3.   

    select v.*,a.title from (select f.id,f.content,f.endtimes from (select *  from (
    select id,uid,content,endtimes from 主贴表 a
    union
    select id,uid,content,fbtimes from 跟贴表 d) c) f where f.name='aa') v left join 主贴表 a on v.id=a.id