title: Merging intervals with MySQL
body: <p>I need some help with merging of overlapping intervals.</p>
  
  <p>I have this table:</p>
  
  <blockquote><pre>
  id  start       end
  1   15:30:00    16:20:00
  2   10:00:00    13:00:00
  3   15:00:00    16:09:00
  4   11:00:00    14:00:00
  5   16:20:00    16:30:00
  </pre></blockquote>
  
  <p>SQL:</p>
  
  <pre><code>CREATE TABLE `intervals` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `start` time NOT NULL,
    `end` time NOT NULL,
    PRIMARY KEY  (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  
  INSERT INTO `intervals` (`start`, `end`) VALUES
  ('15:30:00', '16:20:00'),
  ('10:00:00', '13:00:00'),
  ('15:00:00', '16:09:00'),
  ('11:00:00', '14:00:00'),
  ('16:20:00', '16:30:00');
  </code></pre>
  
  <p>And I want output like this:</p>
  
  <blockquote><pre>
  id  start       end
  2   10:00:00    14:00:00
  3   15:00:00    16:30:00
  </pre></blockquote>
  
  <p>Only the start and end times matter; the ID column can basically be ignored.</p>
  
  <p>There is similar implementation by PHP <a href="https://stackoverflow.com/a/4347215/1085872">https://stackoverflow.com/a/4347215/1085872</a> (until step 2) but I need to do achieve the merge with only MySQL.</p>