被問到的問題是,如何得到不包含重複結果的結果。由於沒有提到任何字段,我會認爲它是主要的註釋字段,它是文本類型的,這意味着你不能在其上使用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
一個更快的選擇是不是DISTINCT使用GROUP BY COMMENT_CONTENT,然後選擇任一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
而且是問題嗎? –
@swapnesh:這不是問題。 –