代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MSSQL
】
列出所有用户的最后一条记录
作者:
Dezai.CN
/ 发布于
2012/5/10
/
586
有两个表,关联字段是userid,每个用户下面有多条记录,怎么列出所有用户的最后一条记录
<div> <span style="color: #008080">--</span><span style="color: #008080">一、按name分组取val最大的值所在行的数据。</span><span style="color: #008080"> --</span><span style="color: #008080">方法1:</span><span style="color: #008080"> </span><span style="color: #0000ff">select</span><span style="color: #000000"> a.</span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb a </span><span style="color: #0000ff">where</span><span style="color: #000000"> val </span><span style="color: #808080">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">max</span><span style="color: #000000">(val) </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb </span><span style="color: #0000ff">where</span><span style="color: #000000"> name </span><span style="color: #808080">=</span><span style="color: #000000"> a.name) </span><span style="color: #0000ff">order</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> a.name </span><span style="color: #008080">--</span><span style="color: #008080">方法2:</span><span style="color: #008080"> </span><span style="color: #0000ff">select</span><span style="color: #000000"> a.</span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb a </span><span style="color: #0000ff">where</span><span style="color: #000000"> </span><span style="color: #808080">not</span><span style="color: #000000"> </span><span style="color: #808080">exists</span><span style="color: #000000">(</span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb </span><span style="color: #0000ff">where</span><span style="color: #000000"> name </span><span style="color: #808080">=</span><span style="color: #000000"> a.name </span><span style="color: #808080">and</span><span style="color: #000000"> val </span><span style="color: #808080">></span><span style="color: #000000"> a.val) </span><span style="color: #008080">--</span><span style="color: #008080">方法3:</span><span style="color: #008080"> </span><span style="color: #0000ff">select</span><span style="color: #000000"> a.</span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb a,(</span><span style="color: #0000ff">select</span><span style="color: #000000"> name,</span><span style="color: #ff00ff">max</span><span style="color: #000000">(val) val </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb </span><span style="color: #0000ff">group</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> name) b </span><span style="color: #0000ff">where</span><span style="color: #000000"> a.name </span><span style="color: #808080">=</span><span style="color: #000000"> b.name </span><span style="color: #808080">and</span><span style="color: #000000"> a.val </span><span style="color: #808080">=</span><span style="color: #000000"> b.val </span><span style="color: #0000ff">order</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> a.name </span><span style="color: #008080">--</span><span style="color: #008080">方法4:</span><span style="color: #008080"> </span><span style="color: #0000ff">select</span><span style="color: #000000"> a.</span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb a </span><span style="color: #0000ff">inner</span><span style="color: #000000"> </span><span style="color: #808080">join</span><span style="color: #000000"> (</span><span style="color: #0000ff">select</span><span style="color: #000000"> name , </span><span style="color: #ff00ff">max</span><span style="color: #000000">(val) val </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb </span><span style="color: #0000ff">group</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> name) b </span><span style="color: #0000ff">on</span><span style="color: #000000"> a.name </span><span style="color: #808080">=</span><span style="color: #000000"> b.name </span><span style="color: #808080">and</span><span style="color: #000000"> a.val </span><span style="color: #808080">=</span><span style="color: #000000"> b.val </span><span style="color: #0000ff">order</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> a.name </span><span style="color: #008080">--</span><span style="color: #008080">方法5</span><span style="color: #008080"> </span><span style="color: #0000ff">select</span><span style="color: #000000"> a.</span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb a </span><span style="color: #0000ff">where</span><span style="color: #000000"> </span><span style="color: #800000; font-weight: bold">1</span><span style="color: #000000"> </span><span style="color: #808080">></span><span style="color: #000000"> (</span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">count</span><span style="color: #000000">(</span><span style="color: #808080">*</span><span style="color: #000000">) </span><span style="color: #0000ff">from</span><span style="color: #000000"> tb </span><span style="color: #0000ff">where</span><span style="color: #000000"> name </span><span style="color: #808080">=</span><span style="color: #000000"> a.name </span><span style="color: #808080">and</span><span style="color: #000000"> val </span><span style="color: #808080">></span><span style="color: #000000"> a.val ) </span><span style="color: #0000ff">order</span><span style="color: #000000"> </span> <span style="color: #000000">方法1: Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID <a.ID) 方法2: select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID 方法3: select * from #T a where ID=(select min(ID) from #T where Name=a.Name) 方法4: select a.* from #T a join #T b on a.Name=b.Name and a.ID>=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 方法5: select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name) 方法6: select * from #T a where (select count(1) from #T where Name=a.Name and ID <a.ID)=0 方法7: select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID) 方法8: select * from #T a where ID!>all(select ID from #T where Name=a.Name) 方法9(注:ID为唯一时可用): select * from #T a where ID in(select min(ID) from #T group by Name) --SQL2005: 方法10: select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID 方法11: select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1 </span> <span style="color: #000000"><span style="color: #0000ff">select</span><span style="color: #000000"> a.userid,a.username,</span><span style="color: #ff00ff">max</span><span style="color: #000000">(b.head) </span><span style="color: #0000ff">as</span><span style="color: #000000"> head </span><span style="color: #0000ff">from</span><span style="color: #000000"> </span><span style="color: #ff00ff">user</span><span style="color: #000000"> a </span><span style="color: #808080">left</span><span style="color: #000000"> </span><span style="color: #808080">join</span><span style="color: #000000"> photo b </span><span style="color: #0000ff">on</span><span style="color: #000000"> a.userid</span><span style="color: #808080">=</span><span style="color: #000000">b.userid </span><span style="color: #0000ff">group</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> a.userid,a.username</span></span> /************明细表中重复数据的过滤*******************/ select SID_ID,VWSG_Name from (select *,min(SID_ID)over(partition by VWSG_Name) as MinID from td_VWShopGoodsInDetail) as T where SID_ID=MinID </div>
试试其它关键字
列出所有用户的最后一条记录
同语言下
.
SQL查询 多列合并成一行用逗号隔开
.
查看存储过程修改时间,最近执行时间
.
设置手动批量删除数据库相关进程
.
获取某个表中特定字段的所有字符串形式
.
SQL 如何去除重复的字符串
.
怎么去掉一个字段中的重复数据
.
String 去除空格 回车 换行 水平制表符
.
SQL查询和替换含有回车,空格,TAB
.
SQL SERVER 查询每日新增用户数量、次留数量
.
判断两个字符串是否存在相同的内容
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3