select id from t a where edition=(select max(edition) from t where name=a.name)
也许最大edition的记录有两条select * from 表 a where id=(select top 1 id from 表 where name=a.name order by edition desc,id desc)
各位的方法都试了,不行。 应该是我没说清楚,那我举个例子,如下是表中的记录 id name edition ----------------------- 1 a 1 2 a 4--< 3 a 3 4 b 1 5 b 3--< 6 b 2 7 c 1 8 c 5--< 9 c 3 10 c 4要找到表中的2,5,8这几个记录,如何写sql语句。
--测试--测试数据 create table 表(id int,name varchar(10),edition int) insert 表 select 1,'a',1 union all select 2,'a',4--< union all select 3,'a',3 union all select 4,'b',1 union all select 5,'b',3--< union all select 6,'b',2 union all select 7,'c',1 union all select 8,'c',5--< union all select 9,'c',3 union all select 10,'c',4 go--查询 select * from 表 a where not exists(select * from 表 where name=a.name and edition>a.edition) go--删除测试 drop table 表/*--测试结果 id name edition ----------- ---------- ----------- 2 a 4 5 b 3 8 c 5(所影响的行数为 3 行) --*/
--测试--测试数据 create table 表(id int,name varchar(10),edition int) insert 表 select 1,'a',1 union all select 2,'a',4--< union all select 3,'a',3 union all select 4,'b',1 union all select 5,'b',3--< union all select 6,'b',2 union all select 7,'c',1 union all select 8,'c',5--< union all select 9,'c',5--<最大的重复了 union all select 10,'c',4 go--查询 select * from 表 a where id=(select top 1 id from 表 where name=a.name order by edition desc,id desc) go--删除测试 drop table 表/*--测试结果id name edition ----------- ---------- ----------- 2 a 4 5 b 3 9 c 5(所影响的行数为 3 行) --*/
where id=(select top 1 id from 表 where name=a.name order by edition desc,id desc)
应该是我没说清楚,那我举个例子,如下是表中的记录
id name edition
-----------------------
1 a 1
2 a 4--<
3 a 3
4 b 1
5 b 3--<
6 b 2
7 c 1
8 c 5--<
9 c 3
10 c 4要找到表中的2,5,8这几个记录,如何写sql语句。
create table 表(id int,name varchar(10),edition int)
insert 表 select 1,'a',1
union all select 2,'a',4--<
union all select 3,'a',3
union all select 4,'b',1
union all select 5,'b',3--<
union all select 6,'b',2
union all select 7,'c',1
union all select 8,'c',5--<
union all select 9,'c',3
union all select 10,'c',4
go--查询
select * from 表 a
where not exists(select * from 表 where name=a.name and edition>a.edition)
go--删除测试
drop table 表/*--测试结果
id name edition
----------- ---------- -----------
2 a 4
5 b 3
8 c 5(所影响的行数为 3 行)
--*/
create table 表(id int,name varchar(10),edition int)
insert 表 select 1,'a',1
union all select 2,'a',4--<
union all select 3,'a',3
union all select 4,'b',1
union all select 5,'b',3--<
union all select 6,'b',2
union all select 7,'c',1
union all select 8,'c',5--<
union all select 9,'c',5--<最大的重复了
union all select 10,'c',4
go--查询
select * from 表 a
where id=(select top 1 id from 表 where name=a.name order by edition desc,id desc)
go--删除测试
drop table 表/*--测试结果id name edition
----------- ---------- -----------
2 a 4
5 b 3
9 c 5(所影响的行数为 3 行)
--*/
楼主是没有测试吧?