MySQL中删除重复数据只保留一条

时间:2022-09-22 22:37:35

用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)

上述方法在MySQL中会报错 You can’t specify target table for update in FROM clause
mysql中You can’t specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:
复制代码 代码如下:

delete from tbl where id in
(
select max(id) from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
)
改写成下面就行了:
复制代码 代码如下:

delete from tbl where id in
(
select a.id from
(
select max(id) id from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
) a
)
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。
项目里用到是这样写的
DELETE
FROM
issue
WHERE
gatherUrl in (
SELECT a.gatherUrl from(
SELECT
gatherUrl
FROM
issue

    GROUP BY
gatherUrl
HAVING
COUNT(gatherUrl) > 1
)a )

AND id NOT IN (
SELECT b.id from (
SELECT MIN(id) id from issue

GROUP BY
gatherUrl
HAVING
COUNT(gatherUrl) > 1
)b )