程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 如何用sql語句查詢和刪除表中重復數據

如何用sql語句查詢和刪除表中重復數據

編輯:關於MYSQL數據庫

1、查詢表中重復數據(單字段)
Select * From 表 Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1)

 

2、刪除表中多余的重復記錄,只留有rowid最小的記錄(單字段)
Delete From 表
Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1 Having Count(字段1) > 1)
     

3、查找表中多余的重復記錄(多個字段)
Select * From 表 a Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1)

4、刪除表中多余的重復記錄,只留有rowid最小的記錄(多個字段)
Delete From 表 a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段2 Having Count(*) > 1)
     
5.刪除多於的重復記錄(單個字段,多個字段)
delete from table where id not in ( select min(id) from table group by name)
或者
delete from table where id not in ( select min(id) from table group by 字段1,字段2)

6.刪除多余的重復記錄(單個字段,多個字段)
delete from table where id in ( select max(id) from table group by name having count(*)>1)
 

5、查找表中多余的重復記錄(多個字段),不包含rowid最小的記錄
Select *
From 表 a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段2 Having Count(*) > 1)


查單個字段的重復次數
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved