I need some help retrieving data from rows in range.
Let's say this is my table:
That's what I need:
I need to sort the rows by ID and then get 9 rows, where the gameID is 1 and winner's unique_id must be in the middle.
$gameID = 1;
$winner = "iii";
9.iii <--- winner in the middle
$b = $db->query("SELECT * FROM test WHERE gameID = 1 AND unique_id = 'iii'");
$res = $b->fetch();
$winnerID = $res['ID'];
$b2 = $db->query("SELECT * FROM test WHERE ID BETWEEN $winnerID-4 AND $winnerID+4 ORDER BY ID ASC");
$data = $b2->fetchAll();
Is this what you're looking for?
It first finds the id of the winner, then selects rows with IDs smaller and greater than 4. (4 + 4 + 1 = 9)
SELECT * FROM rows WHERE id BETWEEN (SELECT @id := id FROM rows WHERE unique_id = "iii") - 4 AND @id + 4