title: Search matrix for all rectangles of given dimensions (select blocks of seats)
body: <p>All,</p>
  
  <p>I have been trying to work out how to select say 15 tickets within a single block of seats.</p>
  
  <p><strong>EDIT</strong>: the problem is - how to find all rectangles of given dimensions (say 3x5 for example) of free seats?</p>
  
  <p><img src="https://i.stack.imgur.com/MEW0s.gif" alt="enter image description here"></p>
  
  <p>The below is my table, and the query selects 4 consecutive seats (or 15 or whatever) which is fine...</p>
  
  <p>But what I want to do is select say 15 seats, these may be split over multiple rows, i.e. 3 x 5, but I would want them to be blocked together i.e.</p>
  
  <pre><code>row 9 ..(some seats)..[5 seats]..(some seats)..
  row 8 ..(some seats)..[5 seats]..(some seats)..
  row 7 ..(some seats)..[5 seats]..(some seats)..
  </code></pre>
  
  <p>I.e. they would be 3 rows all in front of each other. row9 seats 10 to 25, row8 seats 10 to 25, row7 seats 10 to 25.</p>
  
  <p>Also may need to consider  if a block of seats have varying number of seats i.e. a corner block may be in an arc to has more seats at the back than the front.</p>
  
  <p>Any guidance in form of ehnaceing the SQL or some algorithm or some PHP code. I have been wracking my brain for most of the week now.</p>
  
  <pre><code>CREATE TABLE `seats` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `event_id` int(11) DEFAULT NULL,
    `performance` int(11) DEFAULT NULL,
    `block` int(11) DEFAULT NULL,
    `row` int(11) DEFAULT NULL,
    `seat` int(11) DEFAULT NULL,
    `status` int(10) DEFAULT 1,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>My query to-date - which returns combinations of blocks of X seats.</p>
  
  <pre><code>SELECT    a.event_id, a.performance, a.block,
            a.row, a.seat AS start_seat,
            a.seat + (4 - 1) AS end_seat,
            4 AS requested_seats,
            a.id AS start_allocation_id
  FROM      seats a
            LEFT JOIN seats b ON
                a.event_id = b.event_id AND
                a.performance = b.performance AND
                a.block = b.block AND
                a.row = b.row AND
                a.seat &lt; b.seat AND
                b.seat &lt; a.seat + 4 AND
                b.status = 1
  WHERE     a.status = 1 AND
            a.event_id = 1
  GROUP BY  a.seat
  HAVING COUNT(b.seat) + 1 = 4
  ORDER BY performance
  </code></pre>
  
  <p>Thanks in advance, need more info please just ask!</p>