One pass. Put you number in place of ?
. Gives you the number of the seat in the first sequence, when your requirement was met, or NULL
if no sequence found.
SET @FOUND = 0;
SET @SEAT_MATCHED = NULL;
SELECT
IF(@FOUND < ?,
@FOUND := IF(status == 'Booked', 0, @FROM + 1),
@SEAT_MATCHED := IFNULL(@SEAT_MATCHED, seat_no)
)
FROM seats
ORDER BY seat_no
SELECT @SEAT_MATCHED;
More reading: Control Flow Functions, User Variables
NB! This approach is only applicable if there are few records in the analyzed interval!
Update. Perhaps you can store bitmask of booked seats in the row as an integer. For instance, for 16-seats row the number 36884
(1001000000010100
in binary) means 3rd, 5th, 13th and 16th seats are booked. It would cut down MySQL load. And then you could do the code like this:
<?php
header('Content-Type: text/plain');
// data you get from DB
$seats = bindec('1001000000010100');
$num_seats = 16;
// calculate consecutive free seats
$seats_info = array();
for ($i = 0; $i < $num_seats; $i++, $seats >>= 1) {
if ($seats & 1) {
if (isset($first)) {
$seats_info[$first] = $i - $first;
unset($first);
}
}
else {
if (!isset($first)) {
$first = $i;
}
}
}
// output sequences
var_export($seats_info);
?>
This outputs:
array (
0 => 2,
3 => 1,
5 => 7,
13 => 2,
)
0
is the 1st seat.