use tempdb if object_id('tb') is not null drop table tb go create table tb([no] INT,[stuNo] INT,[stuName] varchar(50)) insert into tb select 1,22,'ww' union all select 2,22,'ww' union all select 3,44,'mm' go --select * from tb --显示最小的 select * from tb t where not exists(select 1 from tb where stuNo=t.stuNo and stuName=stuName and [no] < t.no) /* no stuNo stuName ----------- ----------- -------------------------------------------------- 1 22 ww 3 44 mm (2 行受影响) */ --显示最大的 select * from tb t where not exists(select 1 from tb where stuNo=t.stuNo and stuName=stuName and [no] > t.no) /* no stuNo stuName ----------- ----------- -------------------------------------------------- 2 22 ww 3 44 mm */
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
if object_id('tb') is not null drop table tb
go
create table tb([no] INT,[stuNo] INT,[stuName] varchar(50))
insert into tb
select 1,22,'ww' union all
select 2,22,'ww' union all
select 3,44,'mm'
go
--select * from tb
--显示最小的
select * from tb t
where not exists(select 1 from tb where stuNo=t.stuNo and stuName=stuName and [no] < t.no)
/*
no stuNo stuName
----------- ----------- --------------------------------------------------
1 22 ww
3 44 mm
(2 行受影响)
*/
--显示最大的
select * from tb t
where not exists(select 1 from tb where stuNo=t.stuNo and stuName=stuName and [no] > t.no)
/*
no stuNo stuName
----------- ----------- --------------------------------------------------
2 22 ww
3 44 mm
*/