SQl 语句整理
1.在select提取的每個結果前加上一序號列,從1開始,步長為 ...
select?? (select?? count(*)?? from?? blog?A? where?? A.id <=B.id) As UnID,* from?? blog???B
?2.Groupby order by? top 的使用技巧
?
Codedeclare?@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按時間進行降序排列
轉載于:https://www.cnblogs.com/weichuo/archive/2008/12/29/1364285.html
總結
- 上一篇: How To: Team Build 自
- 下一篇: C# 语法练习(3): 运算符