链接查询 步骤: 1、在SchoolDB库中查询出所有年级名称和其对应的课程名称; 2、查询出所有的年级名称对应的班级名,如果某年级没有对应的班级显示为NULL; 3、查询出所有的系部的名称、教师及每位教师的教龄,如果某系部正在筹建没有教师,则教师名和教龄显示为NULL。
时间: 2024-03-25 22:35:37 浏览: 136
1. 查询所有年级名称和对应的课程名称:
```
SELECT grade.grade_name, course.course_name
FROM grade
LEFT JOIN course ON grade.course_id = course.course_id;
```
2. 查询所有年级名称和对应的班级名:
```
SELECT grade.grade_name, class.class_name
FROM grade
LEFT JOIN class ON grade.grade_id = class.grade_id;
```
3. 查询所有系部的名称、教师及每位教师的教龄:
```
SELECT department.department_name, teacher.teacher_name, DATEDIFF(CURDATE(), teacher.hire_date) AS teaching_years
FROM department
LEFT JOIN teacher ON department.department_id = teacher.department_id;
```
如果某系部正在筹建没有教师,则教师名和教龄显示为NULL,可以使用IFNULL函数处理:
```
SELECT department.department_name, IFNULL(teacher.teacher_name, 'NULL') AS teacher_name, IFNULL(DATEDIFF(CURDATE(), teacher.hire_date), 'NULL') AS teaching_years
FROM department
LEFT JOIN teacher ON department.department_id = teacher.department_id;
```
阅读全文