当前位置:编程学习 > C#/ASP.NET >>

求救论坛的帖子新建时间与回复时间的倒序按混合时间排列问题

表:posts       
posts_id   posts_title posts_content  posts_time  
1001        好人一枚       好人       2010-11-10
1002        肚子饿了      开吃啦      2010-11-11
1003        宵夜啦        宵夜喝粥    2010-11-12
1004        好酒当歌      歌手啊      2010-11-13
表:reply
reply_id   reply_content   reply_time  
1001        真的好人?     2001-1-5
1002        弥漫着         2010-11-12
1003        当歌啦         2010-11-13

要求结果显示的为:怎么实现???下面的效果
--倒序排列,两表的格式是不同的
posts_id   posts_title    newtime 
1004       好酒当歌      2010-11-13  ->有回复,按回复时间
1003       宵夜啦        2010-11-12  ->没回复,按建贴时间
1002       肚子饿了      2010-11-11  ->有回复,按回复时间
1001       好人一枚      2010-11-10  ->有回复,按回复时间

--------------------编程问答-------------------- case语句,
没有回复的就把建贴时间补上,
然后按建贴时间排序. --------------------编程问答-------------------- 创建的时候回复时间=创建时间
--------------------编程问答--------------------

select a.posts_id,a.posts_title,(case ISNULL(b.reply_id,0) when 0 then a.posts_time else a.posts_time end ) newtime from Posts a 
left join reply b on b.reply_id = a .posts_id
order by a.posts_id desc
--------------------编程问答-------------------- 建议楼主在帖子表里增加一个字段“最后回复时间” 以后获取帖子列表的时候 不需要做表连接 可以减轻数据库负担 --------------------编程问答-------------------- 不是按帖子的ID倒序排列,
我想实现的是 有回复时间的帖子就取最后的回复时间,没回复时间的帖子就将新建帖子时间补上,
两者混合在一起按时间倒序排列 --------------------编程问答-------------------- 楼主如果你一点脑子都不动的话,谁也没办法帮你,只改一点就可以了嘛!!

select a.posts_id,a.posts_title,
(case ISNULL(b.reply_id,0) when 0 then a.posts_time else a.posts_time end ) newtime from Posts a 
left join reply b on b.reply_id = a .posts_id
order by newtime desc
--------------------编程问答-------------------- 调试了下,按建贴时间倒序排列,但帖子回复之后,帖子位置不变,无法排在第一位,即在倒序排列中,最新回复时间没有将建贴时间替换掉
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,