日期:2014-05-18  浏览次数:20563 次

一个困扰我很久的论坛查询SQL语句
我做了一个论坛   想查出   最初发贴主题,阅读次数,回复次数,最后发表人,最后发贴时间。

我有个主贴表:mainNoteID   主贴ID,topic   主题,subjectmatter   内容,sendtime   发贴时间,replynum   回复次数,visitnum   访问次数,senduserID   发贴人
回贴表:   replynoteid   回贴ID,mainnoteID   主贴ID,topic   回贴主题,subjectmatter   回贴内容,userID   回贴人,replytime   回贴时间。

同时我发贴的时候,同时写入回贴表,把发的主贴作为回贴的第一条数据,为个方便计算楼层数。

请高手赐教,谢谢了

------解决方案--------------------
select a.*,b.userID ,b.replytime
from 主贴表 a
join 回帖表 b on a. mainNoteID = b.mainnoteID
where not exists(select 1 from 回帖表 where mainNoteID = b.mainNoteID
and b.replytime < replytime )
------解决方案--------------------
select a.topic,a.visitnum,a.replynum,b.userID ,b.replytime
from 主贴表 a,回帖表 b
where a. mainNoteID = b.mainnoteID
and not exists(select * from 回帖表 where mainNoteID = b.mainNoteID
and b.replytime < replytime )