1
我試圖通過評論和喜歡的順序來顯示帖子。 有三個表使用此查詢後,評論和像
的表像它列型是守值像或不像。
如何按COUNT與多表的SUM和MINUS訂購
SQL
SELECT (SELECT COUNT(id) AS count_comment
FROM comment WHERE comment.post_id = post.post_id),
(SELECT COUNT(id) AS count_like
FROM like WHERE like.post_id = post.post_id AND like.type = 'like'),
(SELECT COUNT(id) AS count_unlike
FROM like WHERE like.post_id = post.post_id AND like.type = 'unlike'),
post.* FROM post
ORDER BY (count_comment + count_like - count_unlike) DESC;
所以,這是一個例子,當它顯示在頁面上
post_id | comment | like | unlike | (comment+like-unlike)
4 | 5 | 3 | 1 | 7
1 | 2 | 3 | 0 | 5
2 | 1 | 1 | 4 | -2
... | ... | ... | ... | ...
我的問題是我的SQL是很慢的,請建議另一種方式,如果它能。我試圖使用JOIN,但我無法弄清楚它的SQL應該如何,請幫助謝謝。