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

sql清除重復記錄

發布時間: 2022-03-30 05:12:51

1. sql刪除重復數據且只保留一條

在你的查詢sql裡面將子查詢的title改成id,外面用id in(),這樣會得到去重後的數據導出insert語句,將表中數據備份刪除,再將導出的sql執行一下即可,還有一種就是寫刪除sql,delete from table where id not in(select id from table group by title )。

2. SQL 刪除重復數據

首先刪除一張表中可能存在的重復數據:
delete from 表 where 欄位1 in
(select 欄位1 from
(select 欄位1,row_number() over (partition by 欄位1 order by 欄位2 desc) rn from 表)
where rn>1);
以上欄位1為需要刪除的依據欄位,比如說你需要刪除重復的郵箱,那麼欄位1表示郵箱,而欄位2是按照順序你需要保留的記錄,比如說按照時間排序,保留時間最近的那個郵箱。

刪除一張表中的另一個表中已經存在的記錄
delete from 表1 where exists
(selete 1 from 表2 where 表1.欄位=表2.欄位);

3. SQL如何刪除重復的數據行

SQL Server刪除重復行是我們最常見的操作之一,下面就為您介紹六種適合不同情況的SQL Server刪除重復行的方法,供您參考。
1.如果有ID欄位,就是具有唯一性的欄位
delect table tableName where id not in ( select max(id) from table group by col1,col2,col3... )
group by 子句後跟的欄位就是你用來判斷重復的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
2. 如果是判斷所有欄位也可以這樣 ,【對於表中的指定的欄位的進行檢查是否相同】
select * into #temp from tablename group by id1,id2,....
delete tablename
insert into table select * from #temp
drop table #temp
3. 首先去重復,再獲取N*1條數據插入到臨時表中,【對於表中的所有欄位的進行檢查是否相同】,再將原表的數據刪除,然後將臨時表的數據插入到原表,最後刪除臨時表。
select distinct * into #temp from tablename
delete tablename
go
insert tablename select * from #temp
go
drop table #temp

4. 沒有ID的情況
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
5. 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欄位內容相同即表示記錄相同。
6.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

4. 如何刪除SQL列重復記錄

不知道你你的表結構是怎樣的,也不知道你的結果是怎麼查詢出來了!
建議你加個條件就可以了!
Select * from table
where IsNull(部門,'')<>''

部門那列不為空

5. 求助SQL:如何刪除重復數據

例如:記錄雖然存在重復,但是rowid是唯一的,所以在子查詢取得重復行中最小的rowid,刪除重復行中
大於最小的rowid的行,只是保留了最小rowid的行,就是刪除了重復行。
這個語句如果要調優的話,可以在內部查詢中建索引

delete from ttt a where rowid>(select min(rowid) from ttt b where a.name=b.name);

6. SQL中如何刪除重復數據

select
欄位1,欄位2,欄位3
from
table
group
by
欄位1,欄位2,欄位3
having
count(*)>1
用上邊這句能找出所有重復的數據
欄位1,2,3你替換成你表裡的欄位名,如果有更多欄位的話,你就繼續添加,最後group
by的時候不要忘記了
刪除的時候要建立一個臨時表
create
table
new_table
as
select
欄位1,欄位2,欄位3
from
old_table
group
by
欄位1,欄位2,欄位3;
然後刪除原表數據
truncate
table
old_table;
然後把臨時表數據反插回去
insert
into
new_table
select
*
from
old_table;

7. SQL語句,刪除重復記錄。

如果按題目那個樣子,這個表只有一個欄位的話樓上的解答應該是合要求的吧。就是選出來存在一個臨時表裡然後再填回去:
select distinct test_name into #Tmp from test1
drop table test1
select * into test1 from #Tmp
drop table #Tmp
但是如果按正常的情況下這個表總會有個主鍵什麼的吧?比如說是有個自增的ID欄位。這種情況下就可以:
delete test1 where ID not in (select ID from test1 group by test_name)

8. sql查詢去掉重復記錄

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

9. 幾個刪除重復記錄的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=-1where peopleId in (select peopleId from vitae group by peopleId

10. SQL刪除重復數據保留一條

delete table where id--標識列-- not in (select min(id) from table group by Jnum,Wnum)