0

我有以下三个表(observations, aspect_labels, aspects):

观察表

结构

观察表

数据

观测数据

方面标签表

结构

aspect_labels 表

数据

aspect_labels 数据

方面表

结构

方面表

数据

方面数据

这三个表背后的想法是,每个表observation都有一个Main_DevelopmentMain_Positive但也有无限数量的附加Positive方面和Development方面。

因此,例如,如果我现在(在observations表中)输入一个观察值,则Main_Positive可能是Student Enthusiasmaspect_labelID 1)并且Main_Development可能是Student Engagementaspect_labelID 2)。但是,我也可以在表中添加( ID 3)的标准Positive方面。Expectationsaspect_labelaspects

我正在尝试做的事情,按Teacher_ID,是显示每个工作人员aspect在他们的观察中列出的次数。

所以,我想看看D Wraight有 2 Student Engagement, 0 Expectations, 1Student 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

4

1 回答 1

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 进行分组来获取所有计数可能会更快,然后将结果加入到教师表中以获取名称。将避免在连接字段上进行分组。

于 2013-10-15T12:38:50.440 回答