Jump to content

choose nearest value


greatstar00

Recommended Posts

I have a table which holds the integer values(the value representing some data) of many users

the table look like this (MySQL)

id  f1   f2    f3

1   30  50   15

2   31  17   37

3   18  31   54

and more ....

 

 

 

i randomly select a user from the another table(let's call it sel_user), and then i select a user who got the numbers closest to the sel_user or same as the sel_user, from the table above

 

 

here is the method of select closest user,

it is based on the sum of 3 columns, f1, f2, f3, (i.e user 2, so the numbers are 31  17 37)

if a sel_user got values 12  30 18, then the sum is 60

from the table above, user 2 is closest, because 31+17+37 is 85, others are larger than 85

 

 

 

my solution is (with mysql query)

add sum the user's value, put it onto the stack for all users, then compare, abs(sel_user's sum - user_from_table's sum)

 

 

 

 

my question is

is there any faster way to do this?

the method i used gets slower and slower, because the mysql string i sent is longer and longer based on numbers of users

or is there a way to make the mysql string always has same length each time?

Link to comment
Share on other sites

You can add another column, called sum, that will sum f1,f2,f3 and will be updated whenever you change the values of f1,f2,f3.

This way you will only have to check which value is the closest to the values in "sum", and not to start summing each row every time.

 

This will probably make your scripts run faster :)

 

Orio.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.