34,588
社区成员
发帖
与我相关
我的任务
分享
create table tb(名字 char(6),班级 char(6),入学日期 datetime)
insert into tb values( '张三','高二','2006-01-01')
insert into tb values( '李四','高三','2003-01-02')
insert into tb values( '李四','高三','2006-08-02')
insert into tb values( '李四','高三','2004-03-03')
go
delete a from tb a where 入学日期 not in (select max(入学日期) from tb where 名字 = a.名字)
select * from tb
drop table tb
/*
名字 班级 入学日期
------ ------ ------------------------------------------------------
张三 高二 2006-01-01 00:00:00.000
李四 高三 2006-08-02 00:00:00.000
(所影响的行数为 2 行)
*/
delete from tb a where 入学日期 where 入学日期 not in (select max(入学日期) from tb where 名字 = a.名字)
create table tb1130(nm char(6),cls char(6),startday datetime)
insert into tb1130 values ('张三','高二','2006-01-01')
insert into tb1130 values ('李四','高三','2003-01-02')
insert into tb1130 values ('李四','高三','2006-08-02')
insert into tb1130 values ('李四','高三','2004-03-03')
delete t1
from tb1130 t1
where
t1.startday<(select max(startday) from tb1130 where nm=t1.nm group by nm)
select * from tb1130
create table sss(名字 char(6),班级 char(6),入学日期 datetime)
insert sss select '张三','高二','2006-01-01'
insert sss select '李四','高三','2003-01-02'
insert sss select '李四','高三','2006-08-02'
insert sss select '李四','高三','2004-03-03'
delete sss from sss b
where exists( select 1 from sss where 名字=b.名字 and 入学日期>b.入学日期)
create table t1 (
名字 varchar(10),
班级 varchar(10),
入学日期 datetime
)
insert into t1 (名字,班级,入学日期 ) values('张三','高二','2006-01-01')
insert into t1 (名字,班级,入学日期 ) values('李四','高三','2003-01-02')
insert into t1 (名字,班级,入学日期 ) values('李四','高三','2006-08-02')
insert into t1 (名字,班级,入学日期 ) values('李四','高三','2004-03-03')
select * from t1
/*
名字 班级 入学日期
---------- ---------- ------------------------------------------------------
张三 高二 2006-01-01 00:00:00.000
李四 高三 2003-01-02 00:00:00.000
李四 高三 2006-08-02 00:00:00.000
李四 高三 2004-03-03 00:00:00.000
(所影响的行数为 4 行)
*/
select *
from t1 a
where not exists(select * from t1 where 名字=a.名字 and 入学日期>a.入学日期)
/*
名字 班级 入学日期
---------- ---------- ------------------------------------------------------
张三 高二 2006-01-01 00:00:00.000
李四 高三 2006-08-02 00:00:00.000
(所影响的行数为 2 行)
*/
create table tb(名字 char(6),班级 char(6),入学日期 datetime)
insert tb select '张三','高二','2006-01-01'
insert tb select '李四','高三','2003-01-02'
insert tb select '李四','高三','2006-08-02'
insert tb select '李四','高三','2004-03-03'
delete a
from tb a
where exists(select 1
from tb
where a.名字 = 名字 and 入学日期 > a.入学日期)
select * from tb
/*
名字 班级 入学日期
------ ------ ------------------------------------------------------
张三 高二 2006-01-01 00:00:00.000
李四 高三 2006-08-02 00:00:00.000
*/
drop table tb