This is a problem with spaces and islands. Here is one way to solve the problem.
SELECT SUM(minutes) total FROM ( SELECT TIMESTAMPDIFF(MINUTE, MIN(starttime), MAX(endtime)) minutes FROM ( SELECT starttime, endtime, @g := IF(@e BETWEEN starttime AND endtime OR endtime < @e, @g, @g + 1) g, @e := endtime FROM table1 CROSS JOIN ( SELECT @g := 0, @e := NULL ) i ORDER BY starttime, endtime ) q GROUP BY g ) q
Conclusion:
| TOTAL |
| ------- |
| 160 |
Here is the SQLFiddle demo
source share