posttime是发帖时间 id title parentid posttime 1 222 null 2004-08-06 2 333 null 2004-09-09 3 444 1 2004-12-12 4 555 2 2004-12-15 因为id为1、2的都有子帖,所以按它们的子帖的posttime时间将父帖作降序排列,实现的效果就是: id title parentid posttime 2 3333 null 2004-12-15 1 2222 null -
select id,title,parentid,postdate from 表 where id not in (select distinct parentid from 表) and parentid =0 union select 表.id,表.title,表.parentid,maxdate from 表, (select parentid,max(postdate) as maxdate from 表 where parentid<>0 group by parentid) as a where 表.id = a.parentid order by postdate desc ----------------------------- 实现的效果是: ------------------------------- 9 3333 0 2004-12-15 10:05:00.000 7 1111 0 2004-12-15 10:03:00.000 不知道用Null的怎么表示,这里改成0了,楼主自己改回来吧,:)。 小弟在这抛砖头了,写的太复杂,效率肯定不高, 哪位大哥有更好的请指教。
楼上的执行结果:
10 4444 9 4444 2004-12-15 10:02:00
11 5555 7 5555 2004-12-15 10:05:00
8 2222 7 yyyy 2004-12-15 10:02:00
7 1111 NULL 1111 2004-12-15 10:02:00
9 3333 NULL 3333 2004-12-15 10:02:00
降序是达到了,但是parentid为null的应显示在上面,达到的效果要如下:
7 1111 NULL 1111 2004-12-15 10:02:00
9 3333 NULL 3333 2004-12-15 10:02:00
只显示parentid为null的记录,按其子记录(有parentid属于该记录的记录)的posttime作DESC
order parrentid asc,posttime desc
select * from 表 where parentid is null order by (case parentid when null then '99999' end ) desc, posttime desc
没有按每个子帖的最后回复时间排序
10 4444 9 4444 2004-12-15 10:02:007 1111 NULL 1111 2004-12-15 10:02:00
11 5555 7 5555 2004-12-15 10:05:00
8 2222 7 yyyy 2004-12-15 10:02:00这样的吗?
id title parentid posttime
1 222 null 2004-08-06
2 333 null 2004-09-09
3 444 1 2004-12-12
4 555 2 2004-12-15
因为id为1、2的都有子帖,所以按它们的子帖的posttime时间将父帖作降序排列,实现的效果就是:
id title parentid posttime
2 3333 null 2004-12-15
1 2222 null -
union
select 表.id,表.title,表.parentid,maxdate from 表,
(select parentid,max(postdate) as maxdate
from 表 where parentid<>0 group by parentid) as a
where 表.id = a.parentid
order by postdate desc
-----------------------------
实现的效果是:
-------------------------------
9 3333 0 2004-12-15 10:05:00.000
7 1111 0 2004-12-15 10:03:00.000
不知道用Null的怎么表示,这里改成0了,楼主自己改回来吧,:)。
小弟在这抛砖头了,写的太复杂,效率肯定不高,
哪位大哥有更好的请指教。