MySQL以数据库user_db及数据表student、teacher、class、course、sc为基础(5)查询出生日期和身份证号表示的出生日期不一致的学生信息; (6)查询学号和所在年级不一致的学生信息; (7)查询每位学生的学号、姓名、入学年份、出生年份及入学年龄; (8)查询2010级至2013级学生的最大年龄、最小年龄、平均年龄; (9)查询选修“数据库原理”的学生的最高成绩、最低成绩、平均成绩; (10)查询每个院系考试课程的数量,按照课程数量降序排列;
时间: 2023-06-14 17:04:47 浏览: 110
(5) 查询出生日期和身份证号表示的出生日期不一致的学生信息:
```
SELECT * FROM student WHERE DATE_FORMAT(id_card,'%Y-%m-%d') != birthday;
```
(6) 查询学号和所在年级不一致的学生信息:
```
SELECT * FROM student WHERE SUBSTR(stu_id,1,2) != SUBSTR(grade,3,2);
```
(7) 查询每位学生的学号、姓名、入学年份、出生年份及入学年龄:
```
SELECT stu_id,stu_name,SUBSTR(stu_id,1,2) AS entry_year,YEAR(birthday) AS birth_year,YEAR(NOW())-SUBSTR(stu_id,1,2)+1 AS age FROM student;
```
(8) 查询2010级至2013级学生的最大年龄、最小年龄、平均年龄:
```
SELECT MAX(YEAR(NOW())-SUBSTR(stu_id,1,2)+1) AS max_age,MIN(YEAR(NOW())-SUBSTR(stu_id,1,2)+1) AS min_age,AVG(YEAR(NOW())-SUBSTR(stu_id,1,2)+1) AS avg_age FROM student WHERE SUBSTR(stu_id,1,2) BETWEEN '10' AND '13';
```
(9) 查询选修“数据库原理”的学生的最高成绩、最低成绩、平均成绩:
```
SELECT MAX(score) AS max_score,MIN(score) AS min_score,AVG(score) AS avg_score FROM sc WHERE course_name='数据库原理';
```
(10) 查询每个院系考试课程的数量,按照课程数量降序排列:
```
SELECT department,COUNT(DISTINCT course_name) AS course_num FROM sc INNER JOIN student ON sc.stu_id=student.stu_id GROUP BY department ORDER BY course_num DESC;
```
阅读全文