Author Topic: Ranking Algorithm Into Query  (Read 832 times)

0 Members and 1 Guest are viewing this topic.

Offline katlisTopic starter

  • Irregular
  • Posts: 35
    • View Profile
Ranking Algorithm Into Query
« on: February 23, 2010, 04:06:31 PM »
So I have this reddit-esque ranking function...

Code: [Select]
function rank($ts,$z) {

$rating = log10($z) + (-1*$ts)/45000;
return $rating;

}

$z = number of votes
$ts = number of seconds between January 19th and the timestamp the entry was created... so I guess something like TIMESTAMPDIFF(SECOND,'2010-01-19 01:00:00',`post.published`)

I'm pretty lost on how to convert this into a query... I'd greatly appreciate any help anyone can provide on this.
Thanks! I'm using 5.1.43.

Offline katlisTopic starter

  • Irregular
  • Posts: 35
    • View Profile
Re: Ranking Algorithm Into Query
« Reply #1 on: February 23, 2010, 09:40:25 PM »
I think I'm getting there....
Code: [Select]
SELECT LOG10(`votes`)+(-1*(TIMESTAMPDIFF(SECOND,'2010-01-19 11:00:00',`published`))/45000) FROM post ORDER BY `id` ASC;

Atleast I'm getting some sort of data back. Any one have input on how I can clean this up... and at the same time, return a list of the `id`'s ordered by the rating this formula is returning?
Thank you.

EDIT: A little more background on this... I have a list of articles that have votes in my "post" table. The important columns are `published` (the timestamp of publishing), `votes`, and `id`. I want to use the formula to sort through these entries and return the list of IDs ordered by rating of this "algorithm" (in desc).
« Last Edit: February 23, 2010, 09:45:18 PM by katlis »

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Ranking Algorithm Into Query
« Reply #2 on: February 24, 2010, 02:44:12 PM »
If you alias that expression, you should be able to use it in the ORDER BY.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline katlisTopic starter

  • Irregular
  • Posts: 35
    • View Profile
Re: Ranking Algorithm Into Query
« Reply #3 on: February 24, 2010, 04:45:04 PM »
If you alias that expression, you should be able to use it in the ORDER BY.

Any chance you can show me an example of how that's done? I haven't done that before. Did a quick search, but can't seem to figure this one out.

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Ranking Algorithm Into Query
« Reply #4 on: February 25, 2010, 04:20:00 PM »
Code: [Select]
SELECT LOG10(`votes`)+(-1*(TIMESTAMPDIFF(SECOND,'2010-01-19 11:00:00',`published`))/45000) AS ranking FROM post ORDER BY ranking DESC;
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.