2

嗨,我有一张表格,列出了西班牙语课程的开始和结束时间以及日期和老师。我正在尝试编写一个 MYSQL 查询,列出每个老师上课最多的日期,他们最忙的那一天。如果有几天打成平手,我想同时显示。

这是我到目前为止所拥有的,但它只返回一个日期,即使有平局,我觉得这是因为我最后在老师身上分组

SELECT teacher_no, date
FROM (
   SELECT COUNT(date)AS counted,teacher_no,date
   FROM Spanish
   GROUP BY teacher_no,date
) AS t
GROUP BY teacher_no
4

2 回答 2

0

试试这个:

SELECT  teacher_no, date, COUNT(date) AS counted
FROM    Spanish s
GROUP BY teacher_no, date
HAVING  COUNT(date) = (SELECT COUNT(date) AS counted
                       FROM Spanish
                       WHERE teacher_no = s.teacher_no
                       GROUP BY teacher_no, date
                       ORDER BY counted DESC
                       LIMIT 1
                      );
于 2012-11-27T20:14:52.343 回答
0

我不确定您为什么要在内部查询中计算日期。我认为计算开始时间更有意义。这为您提供了每个教师在每个日期上的课程数量的表格:

SELECT COUNT(start_time) AS classes, teacher_no, date
FROM Spanish
GROUP BY tearcher_no, date) AS t

对于特定的 teacher_no X,您可以按课程数量排序以获得最多课程的日期:

SELECT date
FROM (        
    SELECT COUNT(start_time) AS classes, date
    FROM Spanish
    GROUP BY date
    WHERE teacher_no = X) AS t
ORDER BY classes DESC
LIMIT 1

最后,您可以获得所有教师的列表,例如

SELECT
    a.teacher_no,
    (SELECT date
     FROM
         (SELECT COUNT(b.start_time) AS classes, b.date
          FROM Spanish AS b
          GROUP BY b.date
          WHERE a.teacher_no = b.teacher_no) AS t
     ORDER BY classes DESC
     LIMIT 1) AS date
FROM Spanish AS a
GROUP BY a.teacher_no
于 2012-11-27T20:26:24.653 回答