3

我如何通过一个查询获得以下信息:我想从我的评论表中获得所有对给定 post_id 进行评论的人,然后根据用户的姓名检查用户评论了多少次。我想避免对它进行 2 个不同的查询

我一直在尝试以下但不会返回预期结果

SELECT comments.*, COUNT(approved.comment_approved) AS has_commented  FROM wp_comments AS comments  
    INNER JOIN wp_comments AS approved 
    ON comments.comment_author = approved.comment_author

WHERE comments.comment_post_ID =14616
GROUP BY comments.comment_content
4

1 回答 1

3

你不应该分组post_ID吗?(那只会返回一行)

SELECT
  comments.*
, COUNT(approved.comment_approved) AS "has_commented"
FROM wp_comments AS comments
JOIN wp_comments AS approved
  ON (comments.comment_author = approved.comment_author)
WHERE comments.comment_post_ID = 14616
GROUP BY comments.comment_post_ID
;

还是您希望每个“已批准”评论一行?

于 2013-06-03T08:30:23.727 回答