Sql 是否删除Wordpress中的重复注释?

Sql 是否删除Wordpress中的重复注释?,sql,wordpress,comments,Sql,Wordpress,Comments,任何人都知道sql查询或wordpress插件可以帮助我删除重复的评论 当我将帖子、评论导入wordpress时,我遇到了一些超时情况,并重复了这个过程,因此一些评论发布了两次。查看wordpress架构的一些图像,然后您应该能够通过以下查询来确定要删除的记录: SELECT wp_comments.* FROM wp_comments LEFT JOIN ( SELECT MIN(comment_id) comment_id FROM wp_comments GROUP

任何人都知道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
您应该运行上面的查询,并确保它返回正确的记录(将被删除的记录)。一旦您对查询工作感到满意,只需将其从
SELECT
更改为
DELETE

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

我最近遇到了这个问题,最后写了这个小脚本来处理它。这篇文章的好处在于,它还可以让你在每篇文章中获得正确的评论数量。如果只删除重复的注释而不更改此值,则计数将关闭

# 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;
    }
}

哇,这就像一种魅力,一种更具侵略性的形式,我最终用来消除所有重复评论,无论作者或帖子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

这将删除无用的简短评论,这些评论的工作方式类似于模板:“谢谢”、“太好了”…

我尝试了上述所有选项。不幸的是,格里姆杜德的不起作用。TI提供的解决方案删除了两条注释(如果有重复)。我想保留一个副本。在朋友的帮助下,这个脚本成功了

对于任何需要方向的人,这应该是在DB上运行的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;
删除t1
来自wp_评论t1
内部连接wp_注释t2
其中t1.COMMENT\u ID
谢谢。稍后再尝试,我现在遇到了更大的问题。这大致正确,但请注意,许多插件(包括EDD)将在
wp\u comments
表中存储数据。为了防止删除这些内容,我建议在上述查询的末尾添加
comment\u type='comment'
。这会将删除限制为true注释,并保留其他psuedo注释。