wordpress get_results($sql,ARRAY_A)查询注释具有重复注释

wordpress get_results($sql,ARRAY_A)查询注释具有重复注释,sql,wordpress,Sql,Wordpress,使用get_results($sql,ARRAY_A)查询wp_注释时,结果中有注释 下面是sql语句 SELECT comment_ID,comment_parent,comment_author,comment_author_url,comment_author_email,comment_content,comment_date,comment_type,ck_rating_up,ck_rating_down FROM $wpdb->comments LEFT JOIN $com

使用
get_results($sql,ARRAY_A)
查询wp_注释时,结果中有注释

下面是sql语句

SELECT comment_ID,comment_parent,comment_author,comment_author_url,comment_author_email,comment_content,comment_date,comment_type,ck_rating_up,ck_rating_down 
FROM $wpdb->comments 
LEFT JOIN $comment_rating_table 
ON ($wpdb->comments.comment_ID = $comment_rating_table.ck_comment_id) 
WHERE comment_post_ID = $post_id AND comment_approved = 1 
order by comment_id ASC

如何避免重复评论?

问题是,如何获得不包含重复评论的结果。由于没有提到哪个字段,我将假定它是文本类型的主注释字段,这意味着您不能在其上使用DISTINCT

但是,由于此字段包含注释而不是文本,因此实际上不应该将注释字段设置为文本类型。因此,需要将字段从text类型转换为varchar类型,并使用(max)作为字段大小

varchar(max)字段可以保存64k的数据,根据所使用的字母表,大约为10000-20000个单词。这应该足够容纳一篇40页的文章或一条评论

至于SQL:

 SELECT comment_ID,comment_parent,comment_author,comment_author_url,comment_author_email,comment_content,comment_date,comment_type,ck_rating_up,ck_rating_down 
    FROM $wpdb->comments 
    LEFT JOIN $comment_rating_table 
    ON ($wpdb->comments.comment_ID = $comment_rating_table.ck_comment_id) 
    WHERE comment_post_ID = $post_id
    AND comment_approved = 1
    AND comment_ID IN (SELECT DISTINCT( comment_content ) FROM $wpdb->comments 
    LEFT JOIN $comment_rating_table 
    ON ($wpdb->comments.comment_ID = $comment_rating_table.ck_comment_id) 
    WHERE comment_post_ID = $post_id AND comment_approved = 1)
    order by comment_id ASC
一个更快的选项是使用GROUP BY comment_内容,然后选择MIN或MAX,而不是DISTINCT

  SELECT comment_ID,comment_parent,comment_author,comment_author_url,comment_author_email,comment_content,comment_date,comment_type,ck_rating_up,ck_rating_down 
    FROM $wpdb->comments 
    LEFT JOIN $comment_rating_table(
    SELECT MAX(comment_id) AS id FROM comment GROUP BY comment_content
   maxid ON ($wpdb->comments.comment_ID = $comment_rating_table.ck_comment_id)  WHERE comment_post_ID = $post_id AND comment_approved = 1) order by comment_id ASC

@斯瓦普尼什:这不是一个问题。