一张板块表A,一张帖子表B,一张回复表C
A表有ID,还有其他的列先忽略,
B表有ID还有发帖时间列IssueTime和PID,其他的列先忽略,
C表有ID还有还有回复时间列IssueTime和PID,其他的列先忽略。
关联如下
C.PID= B.ID, B.TID = A.ID
查询目标是根据A表的ID查询出和B表关联的前20条数据,且需要根据C表关联的IssueTime来倒序排序
如果B表内有些数据在C表没有关联的数据则根据B表的IssueTime来倒序排序用语言表达就是根据板块ID查询出前20条帖子并且根据回复时间倒序排序,如果无回复则根据发帖时间倒序排序

解决方案 »

  1.   

    你QQ在我这里显示是超级QQ在线,不能给你发信息~
    你发个信息给我看下,我QQ907638015
      

  2.   

    --假设title为B中帖的名称--如果只对A某个ID取前20
    select top 20 * from
    (
    select a.id , b.IssueTime dt , b.title from a , b where a.id = ... and a.id = b.pid and not exists(select 1 from c where c.pid = b.id)
    union all
    select a.id , c.IssueTime dt , b.title from a , b , c where a.id = ... and a.id = b.pid and b.id = c.pid 
    ) t
    order by dt desc--如果是针对A表的所有ID分别取前20个select m.* from 
    (
    select a.id , b.IssueTime dt , b.title from a , b where a.id = b.pid and not exists(select 1 from c where c.pid = b.id)
    union all
    select a.id , c.IssueTime dt , b.title from a , b , c where a.id = b.pid and b.id = c.pid 
    ) m where dt in (select top 20 dt from 
    (
    select a.id , b.IssueTime dt , b.title from a , b where a.id = b.pid and not exists(select 1 from c where c.pid = b.id)
    union all
    select a.id , c.IssueTime dt , b.title from a , b , c where a.id = b.pid and b.id = c.pid 
    ) n where n.id = m.id order by dt desc)
      

  3.   


    SELECT A.* FROM(
    select top 20 * from posts 
    where BelongTopicID=17 order by issuetime desc
    )A left JOIN Replys on a.ID= Replys.BelongPostID
    ORDER BY Replys.IssueTime desc