two closest and two just past a number in PHP <> MySQL

Discussion in 'Programming/Scripts' started by edge, Sep 4, 2008.

  1. edge

    edge Active Member Moderator

    Hi all,

    I hope someone here can show me how to do this.

    I have the following data in a MySQL database.

    ...
    ...
    2454
    4387
    2763
    1998
    4385
    2501
    2529
    2409
    2503
    3082
    ...
    ...

    Reading all the data is no problem, but I would like to show the 1st two numbers closest to a requested number, and the 1st two closest past the requested number.

    Lets say the requested number is: 2500

    So now I want the result to show the two closest in front of 2500 (in the example above that would be 2454 and 2763), AND the two closest just past 2500 (that would be 2501 and 2503)

    Anyone here who might know how I could do this?

    Thank you for any info on this.
     
  2. Ben

    Ben ISPConfig Developer ISPConfig Developer

    You can do it with a query like this:

    It sorts the result descending so that the biggest numbers are on top, and cuts 2 from the top.
    Just change desc into asc and < to > if you want to have the next 2 bigger than 2500.
     
  3. edge

    edge Active Member Moderator

    Looks like that will do the job :)

    Thank you.
     

Share This Page