开发者

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


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";


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


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 ;))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜