Сводная таблица MySQL

 SELECT `name`,
       SUM(IF(`exam`=1,`exam`,NULL)) AS `exam1`,
       SUM(IF(`exam`=2,`exam`,NULL)) AS `exam2`,
       SUM(IF(`exam`=3,`exam`,NULL)) AS `exam3`,
       SUM(IF(`exam`=4,`exam`,0)) AS `exam4`
       FROM `exams` GROUP BY `name`;

SELECT  `SchD` ,
SUM( IF(  `Period` =20150101,  `Period` , NULL ) ) AS  `2015-0101`,
SUM( IF(  `Period` =20150201,  `Period` , NULL ) ) AS  `2015-0201`  
FROM  `tExcel` 
GROUP BY  `SchD` 
LIMIT 0 , 30

Более подробно тут: https://en.wikibooks.org/wiki/MySQL/Pivot_table

Добавить комментарий

Ваш адрес email не будет опубликован.