sql >> Databasteknik >  >> RDS >> Mysql

Transponera resultaten av en MySQL-fråga

Prova detta

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

FIDDLE DEMO



  1. Infoga resultat av en lagrad procedur i en tillfällig tabell

  2. Undvik dessa 4 vanliga DBA-misstag

  3. MySQL hur trunkerar man en tabell i en lagrad procedur?

  4. android JDBC mysql java-anslutningsapp:preDexDebug