我有一个名为P的表,它有两列:id和email。所有的ID都是不同的,并且按升序排列,但有些ID具有相同的电子邮件。我需要删除所有重复的电子邮件,并且只保留ID最小的唯一电子邮件。 正确的查询是:
DELETE FROM P
WHERE id NOT IN
(
SELECT minId FROM
(
SELECT MIN(id) AS minId, email
FROM P
GROUP BY email
) AS Q
)
但是,我使用以下查询,它返回" SyntaxError:near' AS Q'"。有人可以告诉我为什么。感谢。
DELETE FROM P
WHERE id NOT IN
(SELECT MIN(id)
FROM P
GROUP BY email
) AS Q
答案 0 :(得分:0)
删除AS Q不在查询中不再是派生表,现在这是一个子查询。
DELETE FROM P
WHERE id NOT IN
( SELECT MIN(id)
FROM P
GROUP BY email
)
答案 1 :(得分:0)
如果您在第一个查询中的最后一个AS P
大括号末尾添加了)
,则会收到相同的错误。
如果你使用了JOIN或NOT EXISTS
代替IN
DELETE FROM P
WHERE NOT EXISTS
( SELECT 1
FROM P AS p2
GROUP BY p2.email
HAVING MIN(p2.id) = P.id
)
答案 2 :(得分:0)
您不应该为not in
运营商使用别名:
DELETE FROM P
WHERE id NOT IN
( SELECT MIN(id)
FROM P
GROUP BY email
)