The following MySQL query is suppose to rank the posts according to their views + rating + submit date in an ascending order:
select
cat ,
p.id ,
title ,
p.date ,
shares ,
source ,
cat ,
count(v.post_id) views ,
sum(r.ilike) rating,
r.module ,
r.module_id ,
@Rank := @Rank + 1 AS Rank
from
posts p
JOIN
rates r
on
r.module_id = p.id
AND r.module = 'posts'
JOIN
posts_views v
on
v.post_id = p.id
WHERE
p.date <= UNIX_TIMESTAMP(NOW())
AND p.state = '3'
AND
(
p.cat NOT REGEXP '[[:<:]]15[[:>:]]'
)
GROUP BY
r.module_id
ORDER BY
rating DESC ,
views DESC ,
p.date ASC LIMIT 0, 10
We have 3 problems in the result:
- the views column values are doubled
- the rating column values are copying the views' value
- The Rank column in NULL