gpt4 book ai didi

Mysql根据电子邮件删除重复的基础但保留最长的行和长度记录

转载 作者:行者123 更新时间:2023-11-29 17:52:42 25 4
gpt4 key购买 nike

我有这个代码用于根据电子邮件删除重复项并保留较大的 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)

奇怪的是,它适用于小表,但对于我的大表,它只运行一秒钟,没有显示任何效果。谁能告诉我哪里做错了?

最佳答案

你的做法相当危险。它可能适用于 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;

关于Mysql根据电子邮件删除重复的基础但保留最长的行和长度记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49164764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com