I want to get range ranking from a table using mysql query.
the table is like this,
+------------+------+
| first_name | age |
+------------+------+
| Kathy | 2 |
| Jane | 1 |
| Nick | 3 |
| Bob | 5 |
| Anne | 4 |
| Jack | 6 |
| Bill | 8 |
| Steve | 7 |
+------------+------+
and I want to get Jack’s ranking with 2 lower and upper rankers.
+------------+------+
| Anne | 4 |
| Bob | 5 |
| Jack | 6 |
| Steve | 7 |
| Bill | 8 |
+------------+------+
Any idea how to write this query for a MySQL database?
Here is a very ugly way to do this by applying a rownumber to the records. By applying the rownumber to the records, then you you will be able to return rows if the ages are not consecutive (Demo with Non-consecutive ages):
see SQL Fiddle with Demo