在mysql中,设有学生成绩信息表,教师基本信息表,教学任务信息表,查询选修的各门课程的最高成绩、最低成绩和选课人数和授课教师姓名,其中成绩和选修的课程在学生成绩信息表中,授课教师姓名在教师基本信息表中,教学任务信息表设有教师编号与教师基本信息相关联,该如何查询?
时间: 2023-09-19 15:09:59 浏览: 165
您可以使用以下 SQL 语句实现此查询:
```
SELECT
t1.course_name AS 课程名称,
MAX(t1.score) AS 最高成绩,
MIN(t1.score) AS 最低成绩,
COUNT(*) AS 选课人数,
t2.teacher_name AS 授课教师姓名
FROM
score_info AS t1
JOIN course_info AS t2 ON t1.course_id = t2.course_id
JOIN teacher_info AS t3 ON t2.teacher_id = t3.teacher_id
WHERE
t1.course_type = '选修'
GROUP BY
t1.course_name,
t2.teacher_name;
```
其中,score_info 是学生成绩信息表,包括课程编号、学生编号和成绩等信息;course_info 是课程信息表,包括课程编号、课程名称、课程类型和授课教师编号等信息;teacher_info 是教师基本信息表,包括教师编号和教师姓名等信息。
相关问题
使用左外连接查询每个学生的总成绩,要求列出学号、姓名、总成绩,没有选修课程的学生的总成绩为空。mycql
在MySQL(这里假设mycql是你对MySQL语法的一个简称,实际应该写作SQL)中,你可以使用LEFT JOIN来实现这个查询。左外连接会返回左表(通常是学生信息表)的所有记录,即使在右表(课程表)中没有匹配的记录,也会显示,并将对应字段设为NULL。
以下是一个示例的SQL查询:
```sql
SELECT s.student_id, s.student_name, COALESCE(SUM(c.score), 0) AS total_score
FROM students s
LEFT JOIN courses c ON s.student_id = c.student_id
GROUP BY s.student_id, s.student_name;
```
解释一下:
1. `students s` 和 `courses c` 分别代表学生和课程表。
2. `LEFT JOIN` 表示按学生ID关联两个表,如果某个学生没有选课,则课程表c的信息为NULL。
3. `SUM(c.score)` 计算每个学生的总成绩,由于可能有NULL值,所以使用 `COALESCE` 函数将其转换为0(或者其他你想显示的默认值)。
4. `GROUP BY s.student_id, s.student_name` 是为了按照学生ID和姓名分组,这样可以得到每个学生的单独结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)