I have a theme gallery. On the dashboard, I should display the most viewed topics by date (today, last 7 days, last 30 days, all the time).
These are the two tables involved:
The values โโof $ timestamp are calculated using mktime () (there is no problem there).
This is my current SQL query:
SELECT t.id_theme,t.title,
(SELECT COUNT(*)
FROM views
WHERE views.id_theme=t.id_theme
AND views.date BETWEEN '.$timestamp1.' AND '.$timestamp2.')
AS q
FROM theme AS t
INNER JOIN views ON t.id_theme = views.id_theme
GROUP BY views.id_theme
ORDER BY q
DESC LIMIT 10
The problem is that "Catch" is that sometimes it gets topics with 0 views, and this should not happen. I tried changing INNER JOIN with RIGHT JOIN without any results. Any ideas?
source
share