我有以下三个表格(observations
,aspect_labels
,aspects
):
这三个表背后的想法是每个observation
都有一个Main_Development
和Main_Positive
,但也有无数个额外的Positive
方面和Development
方面。
因此,例如,如果我现在输入观察(进入observations
表),Main_Positive
可能是Student Enthusiasm
(aspect_label
ID 1)和{{ 1}}可能是Main_Development
(Student Engagement
ID 2)。不过,我还可以添加aspect_label
(Positive
ID 3)的标准Expectations
方面,该方位进入aspect_label
表。
我正在尝试按aspects
分组,显示每位员工在观察中列出Teacher_ID
每个aspect
的次数。
所以,我想看到D Wraight
有2 Student Engagement
,0 Expectations
,1 Student Enthusiasm
等。
此查询与Main_Positive
表中的observations
方面完全相同,但我不知道如何将aspects
表中的行添加到查询中。
SELECT
CONCAT(sta.Firstname, " ", sta.Surname) AS `Member of Staff`,
SUM(IF(o.`Main_Positive` = 1, 1, 0)) AS `Student enthusiasm`,
SUM(IF(o.`Main_Positive` = 2, 1, 0)) AS `Student engagement`,
SUM(IF(o.`Main_Positive` = 3, 1, 0)) AS `Expectations`,
SUM(IF(o.`Main_Positive` = 4, 1, 0)) AS `Safe and supportive environment`,
SUM(IF(o.`Main_Positive` = 5, 1, 0)) AS `Attitude and values of teacher`,
SUM(IF(o.`Main_Positive` = 6, 1, 0)) AS `Objectives to stretch all students`,
<< MORE GOES HERE >>
FROM frog_observations.observations o
LEFT JOIN frog_shared.staff sta ON o.Teacher_ID = sta.ID
GROUP BY o.Teacher_ID
ORDER BY sta.Surname ASC, sta.Firstname ASC
查询产生此输出(当由PHPMA导出为CSV时):
如何修改查询以包含aspects
表中的联接行?
编辑:SQL小提琴:http://sqlfiddle.com/#!2/75cf3/1/0
答案 0 :(得分:1)
你不能真正做到这一点,因为它会产生可变数量的列(除非你想在每次添加另一个方面时更改SQL)。
您可以这样做: -
SELECT CONCAT(s.Firstname, ' ', Surname), al.Title, IFNULL(SUM(AspectCnt), 0)
FROM staff s
CROSS JOIN aspect_labels al
LEFT OUTER JOIN
(
SELECT o.Teacher_ID, o.Main_Positive AS AspectId, COUNT(*) AS AspectCnt
FROM observations o
GROUP BY o.Teacher_ID, o.Main_Positive
UNION ALL
SELECT o.Teacher_ID, o.Main_Development AS AspectId, COUNT(*) AS AspectCnt
FROM observations o
GROUP BY o.Teacher_ID, o.Main_Development
UNION ALL
SELECT o.Teacher_ID, a.Aspect_ID AS AspectId, COUNT(*) AS AspectCnt
FROM observations o
INNER JOIN aspects a
ON o.ID = a.Observation_ID
GROUP BY o.Teacher_ID, a.Aspect_ID
) Sub1
ON s.ID = Sub1.Teacher_ID
AND al.ID = Sub1.AspectId
GROUP BY CONCAT(s.Firstname, ' ', Surname), al.Title
ORDER BY CONCAT(s.Firstname, ' ', Surname), al.Title
每个方面的每个工作人员为您提供一行,然后在代码中循环以整理您的列。
编辑 - 使用固定列: -
SELECT CONCAT(s.Firstname, ' ', Surname),
IFNULL(SUM(IF(Sub1.AspectId = 1, 1, 0)), 0) AS `Student enthusiasm`,
IFNULL(SUM(IF(Sub1.AspectId = 2, 1, 0)), 0) AS `Student engagement`,
IFNULL(SUM(IF(Sub1.AspectId = 3, 1, 0)), 0) AS `Expectations`,
IFNULL(SUM(IF(Sub1.AspectId = 4, 1, 0)), 0) AS `Safe and supportive environment`,
IFNULL(SUM(IF(Sub1.AspectId = 5, 1, 0)), 0) AS `Attitude and values of teacher`,
IFNULL(SUM(IF(Sub1.AspectId = 6, 1, 0)), 0) AS `Objectives to stretch all students`
FROM staff s
LEFT OUTER JOIN
(
SELECT o.Teacher_ID, o.Main_Positive AS AspectId
FROM observations o
UNION ALL
SELECT o.Teacher_ID, o.Main_Development AS AspectId
FROM observations o
UNION ALL
SELECT o.Teacher_ID, a.Aspect_ID AS AspectId
FROM observations o
INNER JOIN aspects a
ON o.ID = a.Observation_ID
) Sub1
ON s.ID = Sub1.Teacher_ID
GROUP BY CONCAT(s.Firstname, ' ', Surname)
ORDER BY CONCAT(s.Firstname, ' ', Surname)
将它分开以便使用教师ID进行分组来获取所有计数可能会更快,然后将结果返回到教师表以获取名称。会避免在连接字段上进行分组。