Mysql
 SQL >> Database >  >> RDS >> Mysql

Pivotare righe materia in colonne senza tabella temporanea

Usa aggregazione condizionale con CASE:

select s.id, avg(case when sb.id = '01' then s.score end) as math, avg(case when sb.id = '02' then s.score end) as bio from student s join subject sb on sb.id = s.subject_id group by s.id

Ogni CASE restituisce il punteggio solo per la propria materia. Scegli avg o sum in base al dato.