sql选取不重复的数据
我有一个帖子回复表,我现在用同一个用户对同一个帖子进行多次回复操作。然后我想在用户的“我参与的帖子”里看到用户参与的帖子。
根据用户的ID进行选取:
SELECT wb_bbsReply.replyTime, wb_bbsReply.postId, wb_bbsPost.postTitle
FROM wb_bbsReply INNER JOIN
wb_bbsPost ON wb_bbsReply.postId = wb_bbsPost.postId
where wb_bbsReply.userId=@userid
但是这样选取出来的数据是同一个用户对一个贴子的多次回复的数据表。
有没有办法选取出即使当同一个用户对一个贴子的多次回复我也只选取最近的一条数据,而不是用户对帖子的所有回复?
[解决办法]
SELECT top 1 wb_bbsReply.replyTime, wb_bbsReply.postId, wb_bbsPost.postTitleFROM wb_bbsReply INNER JOIN wb_bbsPost ON wb_bbsReply.postId = wb_bbsPost.postId where wb_bbsReply.userId=@useridorder by wb_bbsReply.replyTime desc
[解决办法]
SELECT min(wb_bbsReply.replyTime), min(wb_bbsReply.postId), wb_bbsPost.postTitleFROM wb_bbsReply INNER JOIN wb_bbsPost ON wb_bbsReply.postId = wb_bbsPost.postId where wb_bbsReply.userId=@useridgroup by wb_bbsPost.postTitle
[解决办法]
wb_bbsReply 是一个贴子回复表?
wb_bbsPost 是BBS的贴子列表?
如果是就可以这样试试
Select wb_bbsPost.postTitle,replyTime=(select top 1 replyTime From wb_bbsReply Where wb_bbsReply.postId = wb_bbsPost.postId Order By wb_bbsReply.repluTime Desc ),wb_bbsPost.postIdFrom wb_bbsPostWhere exists (Select * From wb_bbsReply Where wb_bbsReply.postId = wb_bbsPost.postIdand wb_bbsReply.userId=@userid )