Pivot table Schedule


/ Published in: MySQL
Save to your folder(s)

You have a schedule table (period, day, subject, room) with a primary key period,day to avoid duplicate bookings. You wish to display the schedule as periods, subjects and rooms in rows, and days of the week in columns.


Copy this code and paste it in your HTML
  1. period,
  2. MAX(IF(day=1, CONCAT(subject,' ',room), '')) AS Mon,
  3. MAX(IF(day=2, CONCAT(subject,' ',room), '')) AS Tue,
  4. MAX(IF(day=3, CONCAT(subject,' ',room), '')) AS Wed,
  5. MAX(IF(day=4, CONCAT(subject,' ',room), '')) AS Thu,
  6. MAX(IF(day=5, CONCAT(subject,' ',room), '')) AS Fri
  7. FROM schedule
  8. GROUP BY period

Report this snippet


Comments


You need to login to view comments.