1.在select提取的每个结果前加上一序号列,从1开始,步长为 ...
select (select count(*) from blog A where A.id <=B.id) As UnID,* from blog B
2.Groupby order by top 的使用技巧
Code
declare @t table(id int,dat datetime,userid int )
insert into @t select 1,getDate(), 1
union all select 2,getDate()+1,2
union all select 3,getDate()+3,3
union all select 3,getDate()+10,3
union all select 5,getDate()+9,5
select Top 3 userid,max(dat) as dat from @t group by userid order by dat desc
执行顺序:先 group by 再 max,再order by 然后 top
解释:首先用group By 将用户分组,然后用max取出每组时间最大的唯一个UserID,最后将这些UserID按时间进行降序排列