MySQL 没有数据透视函数,但您可以使用带有CASE
表达式的聚合函数来复制它。您的代码将类似于以下内容:
select s.id,
s.name,
max(case when week=1 then control else 'A' end) Week1,
max(case when week=3 then control else 'A' end) Week3,
max(case when week=9 then control else 'A' end) Week9
from students s
inner join attendance a
on s.id = a.student
group by s.id, s.name
请参阅带有演示的 SQL Fiddle
如果week
要返回的值数量未知,则需要在准备好的语句中使用动态 SQL。代码将是:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN week = ',
week,
' THEN control else ''A'' END) AS week',
week
)
) INTO @sql
FROM weeks;
SET @sql
= CONCAT('SELECT s.id,
s.name, ', @sql, '
from students s
inner join attendance a
on s.id = a.student
group by s.id, s.name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
请参阅带有演示的 SQL Fiddle
结果将是:
| ID | NAME | WEEK1 | WEEK3 | WEEK6 | WEEK8 | WEEK9 | WEEK12 |
-----------------------------------------------------------------
| 1 | Charles | P | P | A | A | A | A |
| 2 | Peter | A | A | A | A | A | A |
| 3 | Mary | A | A | A | A | P | A |
注意:如果要返回表中的所有Students
内容,而不管它们在表中是否有匹配的行attendance
,那么您应该使用LEFT JOIN
:
select s.id,
s.name,
max(case when week=1 then control else 'A' end) Week1,
max(case when week=3 then control else 'A' end) Week3,
max(case when week=9 then control else 'A' end) Week9
from students s
left join attendance a
on s.id = a.student
group by s.id, s.name
请参阅SQL Fiddle with Demo。