mysql不同的选择查询在一个

时间:2013-06-03 08:15:51

标签: mysql select

如何使用以下一个查询: 我想从我的评论表中得到所有人如何评论给定的post_id,并根据他的名字检查用户评论了多少时间。我想避免对它有两个不同的查询

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

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

1 个答案:

答案 0 :(得分: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
;

或者您是否希望每个“已批准”评论有一行?