View Full Version : MySQL Query question

20 Nov 2011, 10:04 AM
I'm doing a ranking system which includes searching someone's rank. When the result is returned, I want to show the two people before that person and the two people after that person. I'm not sure of what type of query I would use.

My standard query would be $mysqli->query("SELECT rank,name FROM users WHERE name = '$search' LIMIT 1");
but in this case, I want to also retrieve the other users as well.

Rank Name
50 Person1
51 Person2
54 Person4
55 Person5

21 Nov 2011, 08:43 PM
Some pseudo code for you:

1. Get Rank of Person
SELECT rank, name FROM users WHERE name='$search' LIMIT 1

2. Calculate Min and Max rank
$rank_max = $rank + 3;
$rank_min = $rank - 2;

3. Get Range of People
SELECT rank, name FROM users WHERE rank < $rank_max AND rank > $rank_min LIMIT 5

Get what I mean?