以下是我的数据库结构图:
当我输入观察时,我会在Observations
中输入一行,但在Criteria
中输入0行,1行或更多行。
我正在尝试编写一条SQL语句,允许我选择特定条件最强5位教师。
例如,我点击Questioning
(ID
中可能5
Criteria_Labels
},我想要返回5个列表在Teacher_ID
中排名最多Observations
的教师(Criteria_ID = 5
来自Criteria
)。
我试图写的声明如下:
SELECT t.Name AS Teacher_Name
FROM observations o
LEFT JOIN teachers t ON o.Teacher_ID = t.Teacher_ID
LEFT JOIN criteria c ON o.ID = c.Observation_ID
WHERE c.Criteria_ID = 5
ORDER BY COUNT(c.Criteria_ID) DESC
LIMIT 0,5
但是,它似乎只返回一名工作人员。我不确定我是否已经做到了这一点,但希望我能走到正确的路线。
有人可以帮忙吗?提前谢谢,
答案 0 :(得分:0)
SELECT t.Teacher_ID, t.Name AS Teacher_Name, count(*) as total
FROM observations o
LEFT JOIN teachers t ON o.Teacher_ID = t.Teacher_ID
LEFT JOIN criteria c ON o.ID = c.Observation_ID
WHERE c.Criteria_ID = 5
group by t.Teacher_ID, t.Name
order by total desc
limit 5