id name
1 gate
2 bill
3 bill
4 gate
5 little
6 gate
7 bill
===============output==============
1 gate
2 bill
5 little
我记得以前 有人写过 delete not exists
我始终写不出来了。
13 个解决方案
#1
delete a
from tabname a
where not exists (
select 1
from tabname
where name = a.name
and id < a.id
)
#2
被误导,上面的写错
delete a
from tabname a
where exists (
select 1
from tabname
where name = a.name
and id < a.id
)
#3
--id name
--1 gate
--2 bill
--3 bill
--4 gate
--5 little
--6 gate
--7 bill
--===============output==============
--1 gate
--2 bill
--5 little
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int,name varchar(50))
insert into tb
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
select * from tb t where id=(select MIN(id) from tb where name=t.name) order by id
id name
----------- --------------------------------------------------
1 gate
2 bill
5 little
(3 行受影响)
#4
delete t
from tb t
where exists (select 1 from tb where name = t.name and id > t.id)
#5
--id name
--1 gate
--2 bill
--3 bill
--4 gate
--5 little
--6 gate
--7 bill
--===============output==============
--1 gate
--2 bill
--5 little
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int,name varchar(50))
insert into tb
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
delete tb from tb t where exists (select id,name from tb where name=t.name and id<t.id)
select * from tb
id name
----------- --------------------------------------------------
1 gate
2 bill
5 little
(3 行受影响)
#6
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
select B.id,B.name
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
order by B.id
#7
这个是删除,上面的是查询
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
delete from A
where A.id in
(
select B.id
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
--order by B.id
);
#8
如果是留那3条数据的话
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
delete from A
where A.id not in
(
select B.id
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
--order by B.id
);
#9
delete t from table as t
where exists (select 1 from table where name = t.name and id > t.id)
#10
delete from table where id not in (select min(id) from table)
#11
declare @T table (id int,name varchar(6))
insert into @T
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
select * from @T t where id=
(select min(id) from @T where name=t.name) order by 1
/*
id name
----------- ------
1 gate
2 bill
5 little
*/
#12
declare @T table (id int,name varchar(6))
insert into @T
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
--删除
delete t from @T t where exists
(select top 1 * from @T where name=t.name and id<t.id)
--查询
select * from @T
/*
id name
----------- ------
1 gate
2 bill
5 little
*/
#13
删除重复留最小的id
create table t1(id int,name varchar(10))
insert into t1
select 1, 'gate' union all
select 2, 'bill' union all
select 3, 'bill' union all
select 4, 'gate' union all
select 5, 'little' union all
select 6, 'gate' union all
select 7, 'bill'
go
delete a from t1 as a
where exists (select 1 from t1 where a.name=name and a.id>id)
go
select * from t1
go
drop table t1
#1
delete a
from tabname a
where not exists (
select 1
from tabname
where name = a.name
and id < a.id
)
#2
被误导,上面的写错
delete a
from tabname a
where exists (
select 1
from tabname
where name = a.name
and id < a.id
)
#3
--id name
--1 gate
--2 bill
--3 bill
--4 gate
--5 little
--6 gate
--7 bill
--===============output==============
--1 gate
--2 bill
--5 little
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int,name varchar(50))
insert into tb
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
select * from tb t where id=(select MIN(id) from tb where name=t.name) order by id
id name
----------- --------------------------------------------------
1 gate
2 bill
5 little
(3 行受影响)
#4
delete t
from tb t
where exists (select 1 from tb where name = t.name and id > t.id)
#5
--id name
--1 gate
--2 bill
--3 bill
--4 gate
--5 little
--6 gate
--7 bill
--===============output==============
--1 gate
--2 bill
--5 little
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int,name varchar(50))
insert into tb
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
delete tb from tb t where exists (select id,name from tb where name=t.name and id<t.id)
select * from tb
id name
----------- --------------------------------------------------
1 gate
2 bill
5 little
(3 行受影响)
#6
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
select B.id,B.name
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
order by B.id
#7
这个是删除,上面的是查询
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
delete from A
where A.id in
(
select B.id
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
--order by B.id
);
#8
如果是留那3条数据的话
use tempdb;
/*
create table A
(
id int not null,
name nvarchar(10) not null
);
insert into A values
(1,'gate'),
(2,'bill'),
(3,'bill'),
(4,'gate'),
(5,'little'),
(6,'gate'),
(7,'bill');
*/
delete from A
where A.id not in
(
select B.id
from
(
select *,row_number() over(partition by A.name order by A.id) as [orderno]
from A
) as B
where B.[orderno] = 1
--order by B.id
);
#9
delete t from table as t
where exists (select 1 from table where name = t.name and id > t.id)
#10
delete from table where id not in (select min(id) from table)
#11
declare @T table (id int,name varchar(6))
insert into @T
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
select * from @T t where id=
(select min(id) from @T where name=t.name) order by 1
/*
id name
----------- ------
1 gate
2 bill
5 little
*/
#12
declare @T table (id int,name varchar(6))
insert into @T
select 1,'gate' union all
select 2,'bill' union all
select 3,'bill' union all
select 4,'gate' union all
select 5,'little' union all
select 6,'gate' union all
select 7,'bill'
--删除
delete t from @T t where exists
(select top 1 * from @T where name=t.name and id<t.id)
--查询
select * from @T
/*
id name
----------- ------
1 gate
2 bill
5 little
*/
#13
删除重复留最小的id
create table t1(id int,name varchar(10))
insert into t1
select 1, 'gate' union all
select 2, 'bill' union all
select 3, 'bill' union all
select 4, 'gate' union all
select 5, 'little' union all
select 6, 'gate' union all
select 7, 'bill'
go
delete a from t1 as a
where exists (select 1 from t1 where a.name=name and a.id>id)
go
select * from t1
go
drop table t1