SELECT p.*, u.user_id, u.user_name,
COUNT(c.comment_id) AS count,
COUNT(v.vote_post_id) /
(TIMESTAMPDIFF(MINUTE, v.vote_timestamp, SYSDATE()) + 1) AS rate
FROM posts AS p
LEFT JOIN comments AS c ON (p.post_id = c.comment_post_id)
LEFT JOIN post_votes AS v ON (p.post_id = v.vote_post_id)
LEFT JOIN users AS u ON (p.postedby_id = u.user_id)
GROUP BY p.post_id
ORDER BY COUNT(v.vote_post_id) /
(TIMESTAMPDIFF(MINUTE, v.vote_timestamp, SYSDATE()) + 1) DESC
This is the script I'm working on. I don't have my db filled up very well for testing, but the first two results gets double up with comments. Can you see any obvious mistakes here? I have another version of the script that works fine here:
SELECT p.*, u.user_id, u.user_name,
COUNT(c.comment_id) AS count
FROM posts AS p
LEFT JOIN comments AS c ON (p.post_id = c.comment_post_id)
LEFT JOIN users AS u ON (p.postedby_id = u.user_id)
GROUP BY p.post_id
ORDER BY COUNT(c.comment_post_id) /
(TIMESTAMPDIFF(MINUTE, p.post_timestamp, SYSDATE()) + 1) DESC