Skip to content
Advertisement

Remove null values in MySQL and combine non null values into one cell

Hello guys? I have a MySQL table storing timetable classes, a specific subject is allocated to a specific timeslot stored in columns. I have no issue when there are no two subjects taught at the same time since I use max() to select from the time table i.e table dd

Select dayid,max(`1`) as `1` ,
 max(`2`) as `2` ,
 max(`4`) as `4` ,
 max(`5`) as `5` ,
 max(`7`) as `7` ,
 max(`8`) as `8` ,
 max(`10`) as `10` ,
 max(`11`) as `11` ,
 max(`13`) as `13` ,
 max(`14`) as `14` 
 FROM  dd GROUP BY dd.days ORDER BY dayid

The above query gives me this results Result from query

Now from the first image you can see that the highlighted rows have the same dayid and same timeslot which means they are taught at the same time but my query will only give me one subject (KISW F1A)

I want to achieve this (KISW F1A/BIO F1A) in the cells where classes are taught at the same time.

Advertisement

Answer

Use group_concat()

Select dayid,group_concat(`1`) as `1` ,
 group_concat(`2`) as `2` ,
 group_concat(`4`) as `4` ,
 group_concat(`5`) as `5` ,
 group_concat(`7`) as `7` ,
 group_concat(`8`) as `8` ,
 group_concat(`10`) as `10` ,
 group_concat(`11`) as `11` ,
 group_concat(`13`) as `13` ,
 group_concat(`14`) as `14` 
 FROM  dd GROUP BY dd.days ORDER BY dayid
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement