當前位置:首頁 » 編程語言 » 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的記錄