当前位置: 代码迷 >> Sql Server >> sql选取不重复的数据,该如何处理
  详细解决方案

sql选取不重复的数据,该如何处理

热度:67   发布时间:2016-04-27 13:32:02.0
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 [email protected]
[email protected]?的多次回复的数据表。
有没有办法选取出即使当同一个用户对一个贴子的多次回复我也只选取最近的一条数据,而不是用户对帖子的所有回复?

------解决方案--------------------
SQL code
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 [email protected]order by wb_bbsReply.replyTime desc
------解决方案--------------------
SQL code
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 [email protected]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.postIdFrom wb_bbsPostWhere exists (Select * From wb_bbsReply Where wb_bbsReply.postId = wb_bbsPost.postIdand [email protected]   )
  相关解决方案