Mysql
 sql >> Base de données >  >> RDS >> Mysql

Champs de requête SQL sous forme de colonnes

Essayez,

SELECT  a.Student_name,
        MAX(CASE WHEN subject = 'MATH' THEN grade ELSE NULL END) MathGrade,
        MAX(CASE WHEN subject = 'ENGLISH' THEN grade ELSE NULL END) EnglishGrade,
        MAX(CASE WHEN subject = 'History' THEN grade ELSE NULL END) HistoryGrade
FROM    Student a
        LEFT JOIN Grade b
            ON a.Student_ID = b.Student_ID
GROUP BY a.Student_name

Démo SQLFiddle