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

sql查询重复id

发布时间: 2023-06-15 22:56:18

‘壹’ 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

(1)sql查询重复id扩展阅读

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、查询重复的数据,只查询重复记录,不管其余信息,如ID什么的:

1selectuid,timefromztestGROUPBYuid,timehavingcount(*)>1;

查出结果让春是

uid time

1 1

‘叁’ 求sql多表查询重复数据语法

Create Table tablea(Id Number(12));
Create Table tableb(Id Number(12));
Create Table tablec(Id Number(12));
Insert Into tablea Values(1);
Insert Into tablea Values(2);
Insert Into tablea Values(3);
Insert Into tableb Values(3);
Insert Into tableb Values(4);
Insert Into tableb Values(5);
Insert Into tablec Values(5);
Insert Into tablec Values(6);
Insert Into tablec Values(7);
Commit;
select Id,Count(*) cnt from (
select id from tableA
union all
select id from tableB
union all
select id from tableC ) t Group By Id Having Count(*)>1;

‘肆’ sql查找重复多次的数据

直接查出重复
--查出表中有重复的id的记录,并计算相同id的数量
select id,count(id) from @table group by id having(count(id)>1)
其中,group by id,是按id字段分组查询:
select id,count(id) from @table group by id
可以得到各不同id的数量合计
having(count(id)>1)判断数量大于1,也就是有重复id的记录