views:

84

answers:

1

Let's say I have a screensaver website. I want to display the CURRENT top 100 screensavers on the front page of the website.

What I mean is, "RECENT" top 100 screensavers. What would be an example query to do this?

My current one is:

SELECT * FROM tbl_screensavers WHERE WEEK(tbl_screensavers.DateAdded) = WEEK('".date("Y-m-d H:i:s",strtotime("-1 week"))."') ORDER BY tbl_screensavers.ViewsCount, tbl_screensavers.DateAdded

This will select the most viewed ("tbl_screensavers.ViewsCount") screensavers that were added ("tbl_screensavers.DateAdded") in the last week.

However, in some cases there are no screensavers, or less than 100 screensavers, submitted in that week.

So, how can I perform a query which would select "RECENT" top 100 screensavers? Hopefully you have an idea of what I'm try to accomplish when I say "RECENT" or "CURRENT" top screensavers. -- aka. the most viewed, recently - not the most viewed, all-time.

+1  A: 

Given no other algorithm to weigh the value of a view vs. a recent view, you would just simply want

SELECT * FROM tbl_screensavers ORDER BY ViewsCount limit 100

However, to capture the concept of "recent" you may want to introduce an algorithm to weigh the recent-ness of a particular view. One way to do that is to assign a daysOld score to each view and show the 100 with the lowest score (with this mechanism, low score is good like in golf).

I'm not enough of a MySQL guru to write the query for that, but it would involve summing up the score, computed based on daysOld=today-dateOfScore and then ordering the result set based on that score, with a limit of 100.

Eric J.
Something like:SELECT *, WEEK(DateAdded)*ViewsCount as ViewsValue FROM tbl_screensavers ORDER BY ViewsValue DESC;
Joe
Now if only I knew how to make the above query account for the recent-ness... I am unsure how to determine recent-ness I guess that's my main issue here.
Joe
I suppose what it would need to be is:For each 1x week, ViewsCount should be worth extra points. ??
Joe
or... just simply, for each "day old" it is, the ViewsCount is worth less.know a query for that?
Joe
@Joe: Since you want the most recent views to count the most, you can either give a "penalty" to older views (which is easier math, just give a penalty of 1 per week/day/whatever and say low score is best) or give extra points for being recent (mathematically more challenging)
Eric J.
Yes I'm now trying to determine this penalty by finding the "DaysOld" value of the "DateAdded" field. However, I can't figure it out:http://stackoverflow.com/questions/2497746/mysql-selecting-how-many-days-old-a-field-is
Joe