任何人都知道可以帮助我删除重复评论的 sql 查询或 wordpress 插件。
当我将帖子、评论导入 wordpress 时,我遇到了一些超时和重复的过程,所以有些评论发布了两次。
查看 WordPress 架构的一些图像,然后您应该能够通过查询识别要删除的记录,例如
SELECT wp_comments.*
FROM wp_comments
LEFT JOIN (
SELECT MIN(comment_id) comment_id
FROM wp_comments
GROUP BY comment_post_id, comment_author, comment_content
) to_keep ON wp_comments.comment_id = to_keep.comment_id
WHERE to_keep.comment_id IS NULL
您应该运行上面的查询并确保它返回正确的记录(将被删除的记录)。一旦您对查询感到满意,然后只需将其从 a 更改SELECT
为 aDELETE
DELETE wp_comments
FROM wp_comments
LEFT JOIN (
SELECT MIN(comment_id) comment_id
FROM wp_comments
GROUP BY comment_post_id, comment_author, comment_content
) to_keep ON wp_comments.comment_id = to_keep.comment_id
WHERE to_keep.comment_id IS NULL
哇,这就像一个魅力,我最终用来消除所有重复评论的更积极的形式,无论作者或帖子 ID 将是:
DELETE wp_comments
FROM wp_comments
LEFT JOIN (
SELECT MIN(comment_id) comment_id
FROM wp_comments
GROUP BY comment_content
) to_keep ON wp_comments.comment_id = to_keep.comment_id
WHERE to_keep.comment_id IS NULL
这将删除无用的简短评论,这些评论就像模板一样:“谢谢”、“太好了”......
我最近遇到了这个问题,最后写了这个小脚本来处理它。这样做的好处是它还会为您提供每个帖子的正确评论数。如果您只删除重复的评论而不更改它,那么计数将被取消。
# First select all comments
$query = "SELECT `comment_ID`, `comment_post_ID`, `comment_content` FROM ".$wpdb->comments." WHERE 1";
$comments = $wpdb->get_results($query);
# Array to hold keeper comment IDs so we dont delete them if there are doops
$keeper_comments = array();
# Now check if each comment has any matching comments from the same post
foreach ($comments as $comment) {
$query = "SELECT `comment_ID` FROM ".$wpdb->comments." WHERE `comment_ID` != ".$comment->comment_ID." AND `comment_post_ID` = ".$comment->comment_post_ID." AND `comment_content` = '".addslashes($comment->comment_content)."'";
$matching_comments = $wpdb->get_results($query);
if ($wpdb->num_rows > 0) {
foreach ($matching_comments as $matching_comment) {
if (!in_array($matching_comment->comment_ID, $keeper_comments)) {
$wpdb->query("DELETE FROM ".$wpdb->comments." WHERE `comment_ID` = ".$matching_comment->comment_ID);
$wpdb->query("UPDATE ".$wpdb->posts." SET `comment_count` = `comment_count` - 1 WHERE `comment_ID` = ".$matching_comment->comment_ID);
}
}
$keeper_comments[] = $comment->comment_ID;
}
}
我尝试了上面的所有选项。不幸的是,grimmdude 没有奏效。TI 提供的解决方案删除了两条重复的评论。我想保留其中一个副本。在朋友的帮助下,这个脚本成功了。
对于任何需要指导的人,这应该是在数据库上运行的 SQL 查询。
DELETE t1
FROM wp_comments t1
INNER JOIN wp_comments t2
WHERE t1.COMMENT_ID < t2.COMMENT_ID AND t1.comment_content = t2.comment_content;