SELECT u.name, u.age, u.sex
FROM User u
INNER JOIN (
SELECT age, sex, COUNT(*)
FROM User
GROUP BY age, sex
HAVING COUNT(*) > 1) temp
ON temp.age = u.age
AND temp.sex = u.sex
ORDER BY age, sex
此查询在数据库中找到我所有的重复项,但是如何删除多个重复项?
答案 0 :(得分:1)
选择您仅选择的行中的ID
,然后放入DELETE
,这将删除所有已选择ID
的行。
DELETE FROM User u
WHERE u.ID IN
(
SELECT u.ID
FROM User u
INNER JOIN
(
SELECT u.ID
FROM User
GROUP BY age, sex
HAVING COUNT(*) > 1
) temp
ON temp.age = u.age
AND temp.sex = u.sex
ORDER BY age, sex
)
但是我必须建议您更改查询,因为它会删除所有内容。如果内部连接,则select语句与SELECT * FROM User
相同。所以我想您真正想要的是:
DELETE FROM User u
WHERE u.ID IN
(
SELECT u.ID
FROM User
GROUP BY age, sex
HAVING COUNT(*) > 1
)