发贴表和回贴表在一起,表结构:
PID ID编号
ParentID 发贴 ID
PConetnt 内容
PDate 发贴时间
ParentID为0的是发贴,不为0的为回贴,对应PID现在要查所有发贴,根据回贴最后时间排序
PID ID编号
ParentID 发贴 ID
PConetnt 内容
PDate 发贴时间
ParentID为0的是发贴,不为0的为回贴,对应PID现在要查所有发贴,根据回贴最后时间排序
解决方案 »
- 这个链接打不开?
- sql:两条完全相同的数据怎么用sql语句删除一条??
- button onclick(“将数据库中的数据作为参数”)怎样写
- .net怎么才能接管iis
- 请问大伙有没有做过XML搜索的实例呀
- JavaScript跳转页问题
- 关于在客户端用JS对LISTBOX进行操作,服务器无法获得值的问题的解决,请各位提点建议
- response.redirect("url",false)是否中止了次页面的执行?
- web-service的感觉!
- 谁有象winform中combobox类似的webform控件
- ASP.NET MVC3 使用Entity framework连接ORACLE报错,高手帮忙看看.
- Asp.net如何判断文本框中含有网站链接
(select distinct parentid from tablename where parentid!=0 order by pdate desc)
很明显,这个子查询用了distinct,排序就有问题了
where PDate in (select max(PDate) from table group by ParentID ) )where ParentID='0'
当parentID不为0时,PDate 就为回帖时间了,而不在是发帖时间
利用这种方式构造成两个表进行关联
select * form a, (select 列1,列2 from .....) b where a.字段= b.字段 order by b.字段....
虚构的b表可以查出最后回帖时间...
懒得写了,lz可以再去想想
drop table tab1create table tab1
(
pid int,
parentid int,
pcontent varchar(50),
pdate datetime,
)insert into tab1
select 1,0,'发帖1','2012-8-5' union all
select 2,0,'发帖2','2012-8-5' union all
select 3,1,'回帖1','2012-8-5' union all
select 4,2,'回帖2','2012-8-5' union all
select 5,0,'发帖3','2012-8-6' union all
select 6,5,'回帖3','2012-8-7'select * from tab1select a.pid,a.parentid,a.pcontent,a.pdate,(select max(pdate) from tab1 where parentid=a.pid) reply
from tab1 a where a.parentid =0 order by reply desc