如何删除此复杂查询选择的重复项?

时间:2018-02-11 08:41:11

标签: mysql

我想删除此查询找到的所有重复项的整行。我不想留下任何记录。如何将删除行语法添加到此复杂查询中?

SELECT u.id, u.School, u.Mascot, u.City, u.State
FROM TABLE u
INNER JOIN
(
    SELECT Mascot, City, State, COUNT(*)
    FROM tablename
    GROUP BY Mascot, City, State
    HAVING COUNT(*) > 1
) temp
    ON temp.Mascot = u.Mascot AND
       temp.City = u.City AND
       temp.State = u.State
ORDER BY
    Mascot, City, State;

1 个答案:

答案 0 :(得分:0)

尝试分组,然后仅保留计数为1的组:

SELECT
    u.id, u.School, u.Mascot, u.City, u.State
FROM TABLE u
INNER JOIN
(
    SELECT Mascot, City, State, COUNT(*)
    FROM tablename
    GROUP BY Mascot, City, State
    HAVING COUNT(*) > 1
) temp
    ON temp.Mascot = u.Mascot AND
       temp.City   = u.City   AND
       temp.State  = u.State
GROUP BY
    u.id, u.School, u.Mascot, u.City, u.State
HAVING
    COUNT(*) = 1
ORDER BY
    Mascot, City, State;