I am developing a PHP + MySQL application. I have a table which contains start date and end date of periods. Periods can overlap. Example:
ID Start End
1 01/05/2015 31/05/2015
2 01/06/2015 30/06/2015
3 15/06/2015 25/06/2015 (this record overlaps record with ID 2)
4 17/06/2015 22/06/2015 (this record overlaps records with IDs 2 and 3)
I need to select all records, order by start date and end date, but also split overlapping records and keep their IDs. What i mean is i need to get this in the end:
ID Start End
1 01/05/2015 31/05/2015
2 01/06/2015 14/06/2015
3 15/06/2015 16/06/2015
4 17/06/2015 22/06/2015
3 23/06/2015 25/06/2015
2 26/06/2015 30/06/2015
I can do this by selecting all periods and then sorting/splitting them in PHP, but i am wondering if i can do it on database layer?
Can someone at least point me in the right direction, please?