I want to determine how to get the hours from 2 overlapping datetime ranges. For example using table below:

```
    CREATE TABLE IF NOT EXISTS `schedules` (
      `id` int(11) unsigned NOT NULL auto_increment,
      `start1` datetime NOT NULL,
      `end1` datetime NOT NULL,
      `start2` datetime NOT NULL,
      `end2` datetime NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=37;

    INSERT INTO `schedules` (`id`, `start1`, `end1`, `start2`, `end2`) VALUES
        (1, '2017-04-16T14:00:00', '2017-04-16T23:00:00', '2017-04-16T22:00:00', '2017-04-17T06:00:00'),
        (2, '2017-04-17T17:00:00', '2017-04-18T02:00:00', '2017-04-17T22:00:00', '2017-04-18T06:00:00'),
        (3, '2017-04-18T15:00:00', '2017-04-19T01:00:00', '2017-04-18T22:00:00', '2017-04-19T06:00:00')
        (4, '2017-04-19T22:00:00', '2017-04-20T06:00:00', '2017-04-19T22:00:00', '2017-04-20T06:00:00'),
        (5, '2017-04-20T23:00:00', '2017-04-21T08:00:00', '2017-04-20T22:00:00', '2017-04-21T06:00:00');

```

The result should be should be:

```
   id, Hours
    1,   1
    2,   4
    3,   3
    4,   8
    5,   7

```

I previously got a way using multiple IF statement but I think its running slow. I'll gladly appreciate if you can give me a simple and faster query.