取不同用户的最新数据

来源:互联网 发布:java 线程共享变量 编辑:程序博客网 时间:2024/06/05 20:33

取最新数据很容易,select top  10 * from tb order by id desc 

 

但是要取不同用户的最新数据,就是保证这些数据中不会出现同个用户1条以上的数据

 

select top 10 * from tb a where not exists(select 1 from tb where username=a.username and bpostdate>a.bpostdate) 

 

select top 10 ID,UserName,BTypeID,BTitle,BHPic,BPostDate,LanguageID,BCompanyName,UserType,BRecommend from Business a where BState=1 and BOffline=0 andGetDate() between BPostDate and BValidate and userType>0 and not exists(select 1 from Business where UserName=a.UserName and BPostDate>a.BPostDate)order by userType desc,BPostDate desc

 

http://topic.csdn.net/u/20080906/13/f2c46c3b-9d58-4f95-a6c8-a5d2cd70e112.html?4538700

原创粉丝点击