mathew
mathew

Reputation: 1200

select similar values from mysql database

I have several data s in MySQL database. In my table there is a column called rank. what I want is when some one enter a rank say 25 then the result should display names on similar(+ or -) ranks LIMIT to 10 from table.

example

mathew - 25
john - 26
joe - 25
stewart - 27
kelly - 24
brandon -23
magy - 22 .......etc.

Thanks Mathew

Upvotes: 1

Views: 381

Answers (3)

codaddict
codaddict

Reputation: 455020

You can make use of the MySQL's between and limit clause for this:

$range = 5;  // you'll be selecting around this range. 
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want. 

$query = "select * from table where rank between $min and $max limit $limit";

Upvotes: 5

knittl
knittl

Reputation: 265211

you can use BETWEEN:

SELECT *
  FROM `table`
 WHERE `rank` BETWEEN $input-5 AND $input+5
 LIMIT 10

of course make sure you're input is validated/sanitized or use prepared statements. the code above is pseudocode to explain how you'd do it language agnostic (apart from the sql part ;))

Upvotes: 0

werd
werd

Reputation: 646

SELECT data FROM table WHERE rank>=25 LIMIT 0,10

Upvotes: 0

Related Questions