如一张成绩表中有很多人的成绩,每个人也有很多条记录(每次考试成绩均记录在其中),要查出某人考的最好的一次应如何写??
表a:mz cj
张三 83
李四 71
张三 82
李四 73
张三 83
李四 75
张三 50
李四 68
张三 92
李四 76张三的最好成绩应为:92
表a:mz cj
张三 83
李四 71
张三 82
李四 73
张三 83
李四 75
张三 50
李四 68
张三 92
李四 76张三的最好成绩应为:92
解决方案 »
- 5亿多条记录,怎么样设计使通过号码查询用户信息的速度更快
- 为什么sqlserver2008r2和sqlserver200字符集不样?
- 求一个sql语句!
- jQuery怎么设置设置超链接失效
- 发生时间期间内用药情况
- A表B表的字段結构完全相同,現要將B表的全部內容轉入A表,如何做最快最方便?
- 一个关于库与表的问题
- 请问若干台客户机同时(当然并不可能真正同时)以一个SQLServer2000账号访问服务器端的SQL Server是否可以?若可以,那是否访问时只有进
- 做安装程序时,sql server 客户端文件有哪几个?
- SQL server 2014 安装问题
- 50分,超简单sql语句!
- 什么是SQL server 服务器角色?
a
where mz = '张三'
select mz,max(cj) as cj from a group by mz
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-29 09:59:59
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([mz] varchar(4),[cj] int)
insert [a]
select '张三',83 union all
select '李四',71 union all
select '张三',82 union all
select '李四',73 union all
select '张三',83 union all
select '李四',75 union all
select '张三',50 union all
select '李四',68 union all
select '张三',92 union all
select '李四',76
--------------开始查询--------------------------
select max(cj) as cj from a where mz = '张三'
----------------结果----------------------------
/* cj
-----------
92(1 行受影响)*/
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-29 09:59:59
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([mz] varchar(4),[cj] int)
insert [a]
select '张三',83 union all
select '李四',71 union all
select '张三',82 union all
select '李四',73 union all
select '张三',83 union all
select '李四',75 union all
select '张三',50 union all
select '李四',68 union all
select '张三',92 union all
select '李四',76
--------------开始查询--------------------------
select mz,max(cj) as cj from a where mz = '张三' group by mz
----------------结果----------------------------
/* mz cj
---- -----------
张三 92(1 行受影响)*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([mz] varchar(4),[cj] int)
insert [tb]
select '张三',83 union all
select '李四',71 union all
select '张三',82 union all
select '李四',73 union all
select '张三',83 union all
select '李四',75 union all
select '张三',50 union all
select '李四',68 union all
select '张三',92 union all
select '李四',76select * from [tb] t where cj=(select max(cj) from tb where mz=t.mz)/*
mz cj
---- -----------
张三 92
李四 76*/
select * from tb t
where not exists(select * from tb where name=t.name and cj>t.cj)
mz,MAX(cj) as cj
FROM
TB
GROUP BY
mz
mz,MAX(cj) as cj
FROM
TB
GROUP BY
mz
create table aa
(
mz varchar(8),
cj int
)
goinsert into aa
select '张三', 83 union all
select '李四', 71 union all
select '张三', 82 union all
select '李四', 73 union all
select '张三', 83 union all
select '李四', 75 union all
select '张三', 50 union all
select '李四', 68 union all
select '张三', 92 union all
select '李四', 76 select mz,max(cj)from aa group by mz
--如果是直接找出最大值
select max(cj) from aa