2013-06-03 108 views
3

我怎麼可以有一個查詢以下內容: 我想從我的評論表所有人如何評論一個給定的post_id並比檢查多少時間用戶評論,基於他的名字。我想,以避免有2個不同的查詢它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 
+0

你能告訴我們預期的結果與現在的結果嗎?更好的建立[sqlfiddle](http://sqlfiddle.com/) –

回答

3

你不應該羣? (這將返回只有一行)

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 
; 

還是希望每「批」一行對此有何評論?

+0

非常感謝!這將完成這項工作 – fefe