我有这个代码用于删除基于电子邮件的副本并保留bigt id记录:
DELETE FROM mytable
WHERE id NOT IN (SELECT *
FROM (SELECT MAX(n.id)
FROM mytable n
GROUP BY n.email) x)
现在我根据电子邮件编写此代码,但保留最长的行和长度记录:
DELETE FROM mytable
WHERE (char_length(firstname) + char_length(lastname) + char_length(location) + char_length(address)) NOT IN (SELECT *
FROM (SELECT MAX(char_length(n.firstname) + char_length(n.lastname) + char_length(n.location) + char_length(n.address))
FROM mytable n
GROUP BY n.email) x)
奇怪的是,它适用于一个小桌子,但对于我的大桌子,它只运行一秒并且没有显示任何效果。谁能告诉我哪里做错了?
答案 0 :(得分:0)
你的方法非常危险。它可能适用于ID,但不适用于可能在不同行中共同使用的长度。相反,请使用join
:
DELETE t
FROM mytable t JOIN
(SELECT email,
(char_length(firstname) + char_length(lastname) +
char_length(location) + char_length(address)
) as len
FROM mytable
GROUP BY email
) tt
ON t.email = tt.email
WHERE (char_length(t.firstname) + char_length(t.lastname) +
char_length(t.location) + char_length(t.address)
) <> tt.len;