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

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
但是这样选取出来的数据是同一个用户对一个贴子的多次回复的数据表。
有没有办法选取出即使当同一个用户对一个贴子的多次回复我也只选取最近的一条数据,而不是用户对帖子的所有回复?

------解决方案--------------------
SQL code
SELECT  top 1 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
order by wb_bbsReply.replyTime desc

------解决方案--------------------
SQL code
SELECT min(wb_bbsReply.replyTime), min(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
group by  wb_bbsPost.postTitle

------解决方案--------------------
wb_bbsReply 是一个贴子回复表?
wb_bbsPost 是BBS的贴子列表?

如果是就可以这样试试
SQL code

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.postId
From wb_bbsPost
Where exists (Select * From wb_bbsReply 
Where wb_bbsReply.postId = wb_bbsPost.postId
and wb_bbsReply.userId=@userid
   )