Forum Moderators: open

Message Too Old, No Replies

MySQL: Selecting based on number ranges?

Selecting records based on relevancy of number ranges

         

Ahhk

3:18 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



Hi folks!

I'm trying to figure out the best (any) way to do a relevancy query based on how closely a group of four numbers in a row match a supplied set of numbers.

Basically, in each player's row, I have four game rankings that make up 100 points total. They may, for example, be given 40 for score A, 25 for score B, 20 for C, and 15 for D

And, I want to be able to return a result set that's weighted based on the someone who's got a certain set of scores

In one scenario, I want the results by who's closest to a C of 45 and B of 20

In another scenario, a A of 50, a D of 20, and a B of 5

etc

I want to weight the results based on who is closest to the first selection, then the second, then the third, etc...

Is this even doable? Or do I have to just get ALL of the records and sort with PHP some how? :0

I can restructure the database in any way that would help.

PHP5/MySQL5

TIA!

LifeinAsia

3:36 pm on Sep 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Perhaps something like (for the first one):
SELECT *
FROM YourTable
ORDER BY Abs(45-C), Abs(20-B)

Then the second would be somethign like:
SELECT *
FROM YourTable
ORDER BY Abs(50-A), Abs(20-D), Abs(5-B)

Ahhk

4:46 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



Wow! I was clearly taking the long, scenic, stopping-off-for-a-happy-meal route on that one!

I feel like an idiot now...hehe

Thanks!