当前位置:首页 » 编程语言 » sql查找删除多条件重复记录
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql查找删除多条件重复记录

发布时间: 2022-12-30 03:10:18

sql如何删除重复数据

sql查询去除重复值语句
sql 单表/多表查询去除重复记录
单表distinct
多表group by
group by 必须放在 order by 和 limit之前,不然会报错
************************************************************************************
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>

㈡ SQL查询,如何去除重复的记录

首先,先说明一个问题。这样的结果出现,说明系统设计是有问题的。

其次
删除重复数据,你要提供你是什么数据库
不同数据库会有不同的解决方案。

关键字Distinct 去除重复,如下列SQL,去除Test相同的记录;
1. select distinct Test from Table
2. 如果是要删除表中存在的重复记录,那就逻辑处理,如下:
3. select Test from Table group by Test having count(test)>1
4. 先查询存在重复的数据,后面根据条件删除

还有一个更简单的方法可以尝试一下:
select aid, count(distinct uid) from 表名 group by aid
这是sqlserver 的写法。

  • 如图一在数据表中有两个膀胱冲洗重复的记录。

㈢ sql多条件,删除相同数据

明白了,可以参考如下教程:

用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)

3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId

㈣ sql中怎么删除两条重复记录并保留一条

将数据去重复后暂存到临时表#a中

selectdistinct*into#afromtable1where条件

deletetable1where删除限制条件

insertintotable1select*from#a-将暂存的数据插回数据库

droptable#a-删除临时表

注:当前的数据库,每一个表都应该有一个标志字段,以保证记录不完全重复,否则实用中极易出问题。

(4)sql查找删除多条件重复记录扩展阅读:

SQL语句删除掉重复的其他情况

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

SELECT

*

FROM

people

WHERE

peopleId IN (

SELECT

peopleId

FROM

people

GROUP BY

peopleId

HAVING

count(peopleId) > 1

)

2、查找表中多余的重复记录(多个字段)

SELECT

*

FROM

vitae a

WHERE

(a.peopleId, a.seq) IN (

SELECT

peopleId,

seq

FROM

vitae

GROUP BY

peopleId,

seq

HAVING

count(*) > 1

)

参考资料来源:结构化查询语言(SQL)-网络

㈤ SQL怎样删除重复数据

首先删除一张表中可能存在的重复数据:x0dx0adelete from 表 where 字段1 inx0dx0a(select 字段1 from x0dx0a (select 字段1,row_number() over (partition by 字段1 order by 字段2 desc) rn from 表)x0dx0awhere rn>1);x0dx0a以上字段1为需要删除的依据字段,比如说你需要删除重复的邮箱,那么字段1表示邮箱,而字段2是按照顺序你需要保留的记录,比如说按照时间排序,保留时间最近的那个邮箱。x0dx0ax0dx0a删除一张表中的另一个表中已经存在的记录x0dx0adelete from 表1 where existsx0dx0a(selete 1 from 表2 where 表1.字段=表2.字段);

㈥ sql中如何删除一个表中重复的记录

sql中删除一个表中的重复记录可以采用如下步骤:

1、把a_dist表的记录用distinct去重,结果放到临时表中。

select distinct * into #temp from a_dist;

2、把a_dist表的记录全部删除。

delete from a_dist;

3、把临时表中的数据信息导进到a_dist表中,并删除临时表。

insert into a_distselect * from #temp;

drop table #temp;

(6)sql查找删除多条件重复记录扩展阅读:

SQL (结构化查询语言)是用于执行查询的语法。在数据库上执行的大部分工作都由 SQL 语句完成。SQL 语言包含用于更新、插入和删除记录的语法。

增删改查指令构成了 SQL 的 DML 部分:

  • SELECT- 从数据库表中获取数据

  • UPDATE- 更新数据库表中的数据

  • DELETE- 从数据库表中删除数据

  • INSERT INTO- 向数据库表中插入数据

㈦ SQL查询中如何剔除重复

1,存在两条完全相同的纪录

这是最简单的一种情况,用关键字distinct就可以去掉

example: select distinct * from table(表名) where (条件)

2,存在部分字段相同的纪录(有主键id即唯一键)

如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组

example:

select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])

3,没有唯一键ID

example:

select identity(int1,1) as id,* into newtable(临时表) from table

select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])

drop table newtable

(7)sql查找删除多条件重复记录扩展阅读

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

select * from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

3、查找表中多余的重复记录(多个字段)

select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

㈧ SQL查询中如何剔除重复

1、存在部分字段相同的纪录
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group
代码:select
*
from
table
where
id
in
(select
max(id)
from
table
group
by
[去除重复的字段名列表,....])
2、存在两条完全相同的记录
这是最简单的一种情况,用关键字distinct就可以去掉
代码:select
distinct
*
from
table(表名)
where
(条件)
3、没有唯一键ID
这种较为复杂
代码:
select
identity(int1,1)
as
id,*
into
newtable(临时表)
from
table(原表)
select
*
from
newtable
where
id
in
(select
max(id)
from
newtable
group
by
[去除重复的字段名列表,....])
drop
table
newtable
(8)sql查找删除多条件重复记录扩展阅读:
SQL查询语句
1、查询全部的重复信息
select
*
from
people
where
id
not
in
(
select
min(id)
from
people
group
by
name,sex
HAVING
COUNT(*)
<
2)
2、查询多余的重复信息
select
*
from
people
where
id
not
in
(
select
MIN(id)
from
people
group
by
name,sex)

㈨ SQL语句删除重复的记录

删除重复的数据
delete from tb where id not in (
select id from
(select fileSize,fileName ,max(id) id from tb group by filesize,filename ) a
)

现在完成了重复数据的删除,主要是利用了找出某个分组中最大的那个id,其中包括了所有不重复的id,然后使用not in将需要保留的排除。