用SQL語句,刪除掉重複項只保留一條sql
在幾千條記錄裏,存在着些相同的記錄,如何能用SQL語句,刪除掉重複的呢
一、查找表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷spa
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 )
二、刪除表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄.net
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
DELETEFROM people WHERE peopleName IN ( SELECT peopleName FROM people GROUP BY peopleName HAVINGcount(peopleName) > 1 ) AND peopleId NOT IN ( SELECTmin(peopleId) FROM people GROUP BY peopleName HAVINGcount(peopleName) > 1 )
三、查找表中多餘的重複記錄(多個字段)3d
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
SELECT * FROM vitae a WHERE (a.peopleId, a.seq) IN ( SELECT peopleId, seq FROM vitae GROUP BY peopleId, seq HAVINGcount(*) > 1 )
四、刪除表中多餘的重複記錄(多個字段),只留有rowid最小的記錄code
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
DELETEFROM vitae a WHERE (a.peopleId, a.seq) IN ( SELECT peopleId, seq FROM vitae GROUP BY peopleId, seq HAVINGcount(*) > 1 ) AND rowid NOT IN ( SELECTmin(rowid) FROM vitae GROUP BY peopleId, seq HAVINGcount(*) > 1 )
五、查找表中多餘的重複記錄(多個字段),不包含rowid最小的記錄orm
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
SELECT * FROM vitae a WHERE (a.peopleId, a.seq) IN ( SELECT peopleId, seq FROM vitae GROUP BY peopleId, seq HAVINGcount(*) > 1 ) AND rowid NOT IN ( SELECTmin(rowid) FROM vitae GROUP BY peopleId, seq HAVINGcount(*) > 1 )
6.消除一個字段的左邊的第一位:blog
UPDATE tableName SET [ Title ]= RIGHT ([ Title ],(len([ Title ]) - 1)) WHERE Title LIKE '村%'
7.消除一個字段的右邊的第一位:get
UPDATE tableName SET [ Title ]= LEFT ([ Title ],(len([ Title ]) - 1)) WHERE Title LIKE '%村'
8.假刪除表中多餘的重複記錄(多個字段),不包含rowid最小的記錄string
UPDATE vitae SET ispass =- 1WHERE peopleId IN ( SELECT peopleId FROM vitae GROUP BY peopleId