使用get_results($sql, ARRAY_A)
查询wp_comments时,结果中会有注释。
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
如何避免重复评论?
答案 0 :(得分:0)
问的问题是,如何获得不包含重复的结果。由于没有提到哪个字段,我将假设它是主要的注释字段,它是text类型,这意味着你不能在它上面使用DISTINCT。
然而,由于这个字段包含注释而不是文本的小说,所以实际上应该没有理由将注释字段设置为文本类型。因此,需要将字段从类型文本转换为类型varchar,并使用(max)作为字段大小。
varchar(max)字段可以容纳64k的数据,大约10,000-20,000个字,具体取决于所使用的字母。这应该足以容纳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_content而不是DISTINCT,然后选择MIN或MAX
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