Mysql
 sql >> Database >  >> RDS >> Mysql

Trasporre i risultati di una query MySQL

Prova questo

SELECT  
MAX(CASE WHEN t.CLASS_LESSON = 'class1art' THEN t.ATTENDANTS ELSE NULL END) AS class1art,
MAX(CASE WHEN t.CLASS_LESSON = 'class1history' THEN t.ATTENDANTS ELSE NULL END) AS class1history,
MAX(CASE WHEN t.CLASS_LESSON = 'class2geography' THEN t.ATTENDANTS ELSE NULL END) AS class2geography
FROM 
(
 select 
 group_concat(distinct class, lesson) as class_lesson, count(*) as attendants

 from 
 TableName

 group by 
 class, lesson
) as t

DEMO FIDDLE