即時通訊運行一個查詢,使用3個tabels「發佈,喜歡和評論」我需要得到喜歡和評論帖子已經得到的數量,並在同一時間獲取基本信息職位表,以便即時通訊使用查詢波紋管,但問題是,它copys到commentAmount值likeAmount如果喜歡比較大,除非是評論0與2計數和加入的PHP查詢
SELECT post.*, COUNT(likes.id) as 'LikeAmount', COUNT(comment.id) as 'commentAmount' FROM post
LEFT JOIN likes ON post.id = likes.post
LEFT JOIN comment ON post.id = comment.post
GROUP BY post.id
ORDER BY LikeAmount DESC"
使不工作,但是當我添加不同它的工作,所以當它這樣的時候:
SELECT post.*, COUNT(distinct likes.id) as 'LikeAmount', COUNT(distinct comment.id) as 'commentAmount' FROM post
LEFT JOIN likes ON post.id = likes.post
LEFT JOIN comment ON post.id = comment.post
GROUP BY post.id
ORDER BY LikeAmount DESC";
我不明白爲什麼它的作品具有鮮明的,並與出犯規,並執行不同的馬瑟性能明智的還是它使沒有diffrence sinds它會在一個網站,有TRAFIC的洛特使用..
工程就像一個魅力,感謝它只是沒有給一個0當計數empy,但我可以輕鬆地做到這一點,如果空,謝謝 – Holapress 2012-02-20 14:38:08
COALESCE(pl.like_count,0) – silly 2012-02-20 14:54:05
即時通訊想知道這是否查詢中有2個查詢?如果是這樣不會對性能非常不好的話,那麼它會成爲一個高交通網站 – Holapress 2012-02-21 13:34:48