SQL語句實現重複資料刪除記錄並只保留一條,sql語句

來源:互聯網
上載者:User

SQL語句實現重複資料刪除記錄並只保留一條,sql語句

複製代碼 代碼如下:
delete WeiBoTopics where Id in(select max(Id) from WeiBoTopics group by WeiBoId,Title having COUNT(*) > 1);

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,seq having count(*) > 1) and seq in (select 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)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.