【问题标题】:SQL Select rows with max value from joined tableSQL从连接表中选择具有最大值的行
【发布时间】:2015-12-01 14:03:38
【问题描述】:

我有这 3 个这样的表:

讲师:

+-------------+---------+
| id-lecturer |  name   |
+-------------+---------+
| 1           | Johnson |
| 2           | Smith   |
| ...         | ...     |
| ...         | ...     |
+-------------+---------+

主题:

+------------+---------+
| id-subject |  name   |
+------------+---------+
| 1          | Math    |
| 2          | Physics |
| ...        | ...     |
| ...        | ...     |
+------------+---------+

考试:

+---------+-------------+------------+------------+
| id-exam | id-lecturer | id-subject |    date    |
+---------+-------------+------------+------------+
| 1       | 5           | 1          | 1990-05-05 |
| 2       | 7           | 1          | ...        |
| 3       | 5           | 3          | ...        |
| ...     | ...         | ...        | ...        |
+---------+-------------+------------+------------+

当我尝试做第一个选择时:

SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM exams e    
JOIN subjects s ON e.`id-subject`=s.`id-subject`  
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`  
GROUP BY e.`id-lecturer`, e.`id-subject`  

我得到了正确的答案。它显示了类似的内容:

+-------------+------------+-----------+
| id-lecturer | id-subject | exams-num |
+-------------+------------+-----------+
|        0001 |          1 |         4 |
|        0001 |          3 |         1 |
|        0001 |          4 |         1 |
|        0001 |          5 |         1 |
|        0002 |          1 |         2 |
|        0002 |          2 |         1 |
|        0002 |          4 |         1 |
|        0002 |          6 |         3 |
+-------------+------------+-----------+

现在我只想显示每位讲师的最大人数,我的代码是:

SELECT it.`id-lecturer`, it.`id-subject`, MAX(it.`exams-num`) AS `exams-number`  
FROM (  
   SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`  
   FROM egzaminy e  
   JOIN subjects s ON e.`id-subject`=s.`id-subject`  
   JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`  
   GROUP BY e.`id-lecturer`, e.`id-subject`) it
GROUP BY it.`id-lecturer`

输出:

+-------------+------------+--------------+
| id-lecturer | id-subject | exams-number |
+-------------+------------+--------------+
|        0001 |          1 |            4 |
|        0002 |          1 |            3 |
|        0003 |          1 |            2 |
|        0004 |          1 |            5 |
|        0005 |          2 |            1 |
+-------------+------------+--------------+

我得到每个讲师的最大值的正确数字,但主题 ID 不匹配,它总是采用第一行的 ID。我怎样才能使它正确匹配每一行中的这两个字段?

【问题讨论】:

  • 不要使用'-'。这将导致一个痛苦的世界
  • 感谢您的建议。幸运的是,这是我的大学讲师数据库。
  • 一个恶毒的人,这是肯定的。
  • 我试过了,结果还是一样。

标签: mysql select join count max


【解决方案1】:

我想您可以简单地使用相同的查询来获取更多条件,如下所示。

Select t.Lecturer_id,max(t.exams-num) from
    (SELECT e.id-lecturer as Lecturer_id, e.id-subject as Subject_id,
            COUNT(e.id-lecturer) AS exams-num
    FROM exams e    
    JOIN subjects s ON e.id-subject=s.id-subject  
    JOIN lecturers l ON e.id-lecturer=l.id-lecturer  
    GROUP BY e.id-lecturer, e.id-subject ) as t
    group by t.Lecturer_id

【讨论】:

  • 谢谢,我已经改了,但是我的问题还是没有解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多