當前位置:首頁 » 編程語言 » 刪除表中重復的記錄sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

刪除表中重復的記錄sql

發布時間: 2023-01-12 03:15:25

1. sql命令如何刪除一個表中相同記錄

推薦 刪除重復數據

一、具有主鍵的情況

a.具有唯一性的欄位id(為唯一主鍵)

delect table

where id not in

(

select max(id) from table group by col1,col2,col3...

)

group by 子句後跟的欄位就是你用來判斷重復的條件,如只有col1,

那麼只要col1欄位內容相同即表示記錄相同。

b.具有聯合主鍵

假設col1+','+col2+','...col5 為聯合主鍵

select * from table where col1+','+col2+','...col5 in (

select max(col1+','+col2+','...col5) from table

where having count(*)>1

group by col1,col2,col3,col4

)

group by 子句後跟的欄位就是你用來判斷重復的條件,

如只有col1,那麼只要col1欄位內容相同即表示記錄相同。

or

select * from table where exists (select 1 from table x where table.col1 = x.col1 and

table.col2= x.col2 group by x.col1,x.col2 having count(*) >1)

c:判斷所有的欄位

select * into #aa from table group by id1,id2,....

delete table

insert into table

select * from #aa

二、沒有主鍵的情況

a:用臨時表實現

select identity(int,1,1) as id,* into #temp from ta

delect #temp

where id not in

(

select max(id) from # group by col1,col2,col3...

)

delete table ta

inset into ta(...)

select ..... from #temp

b:用改變表結構(加一個唯一欄位)來實現

alter table 表 add newfield int identity(1,1)

delete 表

where newfield not in

(

select min(newfield) from 表 group by 除newfield外的所有欄位

)

alter table 表 drop column newfield

2. SQL中表裡面怎麼刪除重復數據

出現這種情況的原因是你的表沒有建立關鍵字,當出現重復數據時,sqlserver自帶的圖形化工具刪除就會出現你出現的問題,即不能刪除也不能更新,你可以使用如下方法解決:
1、給表建立關鍵字,比如增加一列自增的欄位,這時候就可以刪除了,刪除完成後再刪除新增的列即可
2、不增加欄位,使用delete語句刪除,但是這種情況會刪除符合條件的數據,包括重復的數據
3、推薦使用1的方法

3. sql資料庫中出現重復行數據,如何刪除這些重復記錄

示例

假設存在一個產品信息表Procts,其表結構如下:

CREATETABLEProcts(
ProctIDint,
ProctNamenvarchar(40),
Unitchar(2),
UnitPricemoney
)

表中數據如圖:

*fromProcts_tempdroptableProcts_temp


這樣就完成了對表中重復記錄的刪除。無論表有多大,它的執行速度都是相當快的,而且因為幾乎不用寫語句,所以它也是很安全的

4. 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(*)>

5. sql中怎麼刪除兩條重復記錄並保留一條

將數據去重復後暫存到臨時表#a中

selectdistinct*into#afromtable1where條件

deletetable1where刪除限制條件

insertintotable1select*from#a-將暫存的數據插回資料庫

droptable#a-刪除臨時表

註:當前的資料庫,每一個表都應該有一個標志欄位,以保證記錄不完全重復,否則實用中極易出問題。

(5)刪除表中重復的記錄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)-網路

6. 如何用SQL語句刪除兩個表中相同的記錄

1,首先創建一個表,並在表中插入重復的記錄,如下圖所示。

7. sql查詢去掉重復記錄

1、打開要去掉重復數據的資料庫,這里新建一張含有重復數據的user表做示例,如下圖所示: