Para este dataset:
CREATE TABLE horarios(
id_horario int AUTO_INCREMENT PRIMARY KEY,
DAY varchar(15),
initime time,
endtime time,
descripcion varchar(31)
);
INSERT INTO horarios (DAY, initime, endtime, descripcion) VALUES
('Lunes','9:00','10:00','Matemáticas'),
('Lunes','10:00','11:00','Comunicación'),
('Martes','9:00','10:00','Java'),
('Martes','10:00','11:00','Python'),
('Miércoles','9:00','10:00','Matemáticas'),
('Miércoles','10:00','11:00','Comunicación'),
('Jueves','9:00','10:00','Java'),
('Jueves','10:00','11:00','Python');
Con tu consulta se obtendría un resultado así:
==============================================================================
|Hora |Lunes |Martes |Miercoles |Jueves |Viernes |Sabado |
==============================================================================
|09:00:00 |Matemática| | | | | |
|- |s | | | | | |
|10:00:00 | | | | | | |
------------------------------------------------------------------------------
|10:00:00 |Comunicaci| | | | | |
|- |ón | | | | | |
|11:00:00 | | | | | | |
------------------------------------------------------------------------------
|09:00:00 | |Java | | | | |
|- | | | | | | |
|10:00:00 | | | | | | |
------------------------------------------------------------------------------
|10:00:00 | |Python | | | | |
|- | | | | | | |
|11:00:00 | | | | | | |
------------------------------------------------------------------------------
|09:00:00 | | |Matemática| | | |
|- | | |s | | | |
|10:00:00 | | | | | | |
------------------------------------------------------------------------------
|10:00:00 | | |Comunicaci| | | |
|- | | |ón | | | |
|11:00:00 | | | | | | |
------------------------------------------------------------------------------
|09:00:00 | | | |Java | | |
|- | | | | | | |
|10:00:00 | | | | | | |
------------------------------------------------------------------------------
|10:00:00 | | | |Python | | |
|- | | | | | | |
|11:00:00 | | | | | | |
------------------------------------------------------------------------------
Para que, para la misma hora en diferentes días, se muestre una única línea, he optado por agrupar por horas generando una columna diferente para cada día:
SELECT * FROM (
SELECT DISTINCT initime,endtime FROM horarios
) horas LEFT JOIN (
SELECT initime,endtime,
GROUP_CONCAT(descripcion) lunes
FROM horarios WHERE DAY='Lunes'
GROUP BY 1,2
) lunes USING(initime,endtime) LEFT JOIN (
SELECT initime,endtime,
GROUP_CONCAT(descripcion) martes
FROM horarios WHERE DAY='Martes'
GROUP BY 1,2
) martes USING(initime,endtime) LEFT JOIN (
SELECT initime,endtime,
GROUP_CONCAT(descripcion) miercoles
FROM horarios WHERE DAY='Miércoles'
GROUP BY 1,2
) miercoles USING(initime,endtime) LEFT JOIN (
SELECT initime,endtime,
GROUP_CONCAT(descripcion) jueves
FROM horarios WHERE DAY='Jueves'
GROUP BY 1,2
) jueves USING(initime,endtime) LEFT JOIN (
SELECT initime,endtime,
GROUP_CONCAT(descripcion) viernes
FROM horarios WHERE DAY='Viernes'
GROUP BY 1,2
) viernes USING(initime,endtime)
ORDER BY horas.initime,horas.endtime;
Obtenemos este resultado:
==============================================================================
|initime |endtime |lunes |martes |miercoles |jueves |viernes |
==============================================================================
|09:00:00 |10:00:00 |Matemática|Java |Matemática|Java |null |
------------------------------------------------------------------------------
|10:00:00 |11:00:00 |Comunicaci|Python |Comunicaci|Python |null |
------------------------------------------------------------------------------