3
好了,就可以了,但我無法查詢;)的MySQL COUNT不能指望
這裏是我的查詢:
SELECT code.id AS codeid, code.title AS codetitle, code.summary AS codesummary, code.author AS codeauthor, code.date, code.challengeid, ratingItems.*, FORMAT((ratingItems.totalPoints/ratingItems.totalVotes), 1) AS rating, code_tags.*, tags.*, users.firstname AS authorname, users.id AS authorid, GROUP_CONCAT(tags.tag SEPARATOR ', ') AS taggroup,
COUNT(DISTINCT comments.codeid) AS commentcount
FROM (code)
JOIN code_tags ON code_tags.code_id = code.id
JOIN tags ON tags.id = code_tags.tag_id
JOIN users ON users.id = code.author
LEFT JOIN comments ON comments.codeid = code.id
LEFT JOIN ratingItems ON uniqueName = code.id
WHERE `code`.`approved` = 1
GROUP BY code_id
ORDER BY date desc
LIMIT 15
重要的線是第二個 - 一個我已經縮進。我要求它計算特定帖子上的評論數量,但不會返回正確的數字。例如,有兩個註釋的東西將返回「1」。有兩個不同作者的評論8仍然會返回「1」...
任何想法?
謝謝!
傑克
編輯:忘了提。當我刪除DISTINCT部分時,來自兩位作者的8條評論返回「28」。對不起,我不是一個MySQL的專家,真的不明白爲什麼它返回的是:(
輝煌,謝謝s Yacoder - 我現在使用'COUNT(DISTINCT comments.id)'這很好用:) – Jack 2010-09-05 12:38:01