MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条
在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
SELECT*FROM peopleWHERE peopleId IN (
SELECT peopleId
FROM people
GROUPBY peopleId
HAVINGcount(peopleId)>1 )
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
DELETEFROM peopleWHERE peopleName IN (
SELECT peopleName
FROM people
GROUPBY peopleName
HAVINGcount(peopleName)>1 )ANDpeopleIdNOTIN (
SELECTmin(peopleId)
FROM people
GROUPBY peopleName
HAVINGcount(peopleName)>1)
3、查找表中多余的重复记录(多个字段)
SELECT*FROM vitae aWHERE (a.peopleId, a.seq) IN (
SELECT peopleId,
seq
FROM vitae
GROUPBY peopleId,
seq
HAVINGcount(*)>1 )
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
DELETEFROM vitae aWHERE (a.peopleId, a.seq) IN (
SELECT peopleId,
seq
FROM vitae
GROUPBY peopleId,
seq
HAVINGcount(*)>1 )ANDrowidNOTIN (
SELECTmin(rowid)
FROM vitae
GROUPBY peopleId,
seq
HAVINGcount(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
SELECT*FROM vitae aWHERE (a.peopleId, a.seq) IN (
SELECT peopleId,
seq
FROM vitae
GROUPBY peopleId,
seq
HAVINGcount(*)>1 )ANDrowidNOTIN (
SELECTmin(rowid)
FROM vitae
GROUPBY peopleId,
seq
HAVINGcount(*)>1)
6.消除一个字段的左边的第一位:
UPDATE tableNameSET[ Title ]=RIGHT([ Title ],(len([ Title ])-1))WHERE Title LIKE'村%'
7.消除一个字段的右边的第一位:
UPDATE tableNameSET[ Title ]=LEFT([ Title ],(len([ Title ])-1))WHERE Title LIKE'%村'
8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
UPDATE vitaeSETispass=-1WHERE peopleId IN (
SELECT peopleId
FROM vitae
GROUPBY peopleId