2017-03-18 67 views
1

我試圖根據他們的AVG(正面投票 - 消極選票)有多好......來顯示我的帖子的評論,但是在嘗試構建查詢時遇到困惑。我需要將它們顯示爲:最開始時的最佳AVG,直到最後的最差AVG,就像後代一樣。如何根據平均計算對mysql查詢結果進行分組?

這裏是我用做查詢:

SELECT c.user AS user, DATE_FORMAT(c.date,'%d de %b a las %h:%i %p') AS datee, c.comment AS comment, COUNT(DISTINCT l.id_like) AS likes, COUNT(DISTINCT d.id_dislike) AS dislikes, AVG(likes - dislikes) AS avg FROM comments c LEFT JOIN likes l ON l.id_comment=c.id_comment LEFT JOIN dislikes d ON c.id_comment=d.id_comment WHERE c.id_post= ? AND l.id_like > 0 GROUP BY c.id_comment DESC LIMIT 10 

但不工作,我想

回答

0

也許這將有助於如果計算爲差異的思想而不是平均值

SELECT c.user AS user, 
     DATE_FORMAT(c.date,'%d de %b a las %h:%i %p') AS datee, 
     c.comment AS comment, 
     COUNT(DISTINCT l.id_like) AS likes, 
     COUNT(DISTINCT d.id_dislike) AS dislikes, 
     (COUNT(DISTINCT l.id_like) - COUNT(DISTINCT d.id_dislike)) as Difference 
FROM comments c LEFT JOIN 
    likes l 
    ON l.id_comment = c.id_comment LEFT JOIN 
    dislikes d 
    ON c.id_comment = d.id_comment 
WHERE c.id_post= ? AND l.id_like > 0 
GROUP BY c.user, datee, c.id_comment DESC 
ORDER BY Difference DESC 
LIMIT 10; 
+0

這就是我所說的!它像一個魅力工作,感謝兄弟:P – calljhon