On my blog, I display in the right nav the 10 most popular articles in terms of page hits. Here's how I get that:
SELECT *
FROM entries
WHERE is_published = 1
ORDER BY hits DESC, created DESC
LIMIT 10
What I would like to do is show the top 10 in terms of page hits per day. I'm using MySQL. Is there a way I can do this in the database?
BTW, The created
field is a datetime.
UPDATE: I think I haven't made myself clear. What I want is for the blog post with 10,000 hits that was posted 1,000 days ago to have the same popularity as the blog post with 10 hits that was posted 1 day ago. In pseudo-code:
ORDER BY hits / days since posting
...where hits
is just an int that is incremented each time the blog post is viewed.
OK, here's what I'm going to use:
SELECT *, AVG(
hits / DATEDIFF(NOW(), created)
) AS avg_hits
FROM entries
WHERE is_published = 1
GROUP BY id
ORDER BY avg_hits DESC, hits DESC, created DESC
LIMIT 10
Thanks, Stephen! (I love this site...)