【10】查询练习:条件加分组查询、not like、year、now、max、子查询、按等级查询

1、查询有任课教师的姓名和department:

课程表:

mysql> select * from course;
+----------+-----------+---------+
| cour_num | cour_name | tea_num |
+----------+-----------+---------+
| 1-245    | Math      | 0438    |
| 2-271    | Circuit   | 0435    |
| 3-105    | OS        | 0435    |
| 4-321    | Bio       | 0436    |
+----------+-----------+---------+

教师表:

mysql> select * from teacher;
+---------+-------------+---------+---------------------+----------+------------+
| tea_num | tea_name    | tea_sex | tea_birth           | tea_prof | department |
+---------+-------------+---------+---------------------+----------+------------+
| 0435    | LiMei       | F       | 1983-02-24 00:00:00 | prof     | Computer   |
| 0436    | MaDi        | F       | 1984-01-23 00:00:00 | assist   | Bio        |
| 0437    | LiZhe       | F       | 1974-01-23 00:00:00 | prof     | Econ       |
| 0438    | ShaoGuoYing | F       | 1985-06-17 00:00:00 | prof     | Math       |
| 0439    | Susan       | F       | 1985-07-18 00:00:00 | assist   | Math       |
| 0440    | Mary        | F       | 1990-05-02 00:00:00 | lecturer | Econ       |
+---------+-------------+---------+---------------------+----------+------------+

我的:

mysql> select cour_num,tea_name,department from course,teacher
    -> where course.tea_num=teacher.tea_num;
+----------+-------------+------------+
| cour_num | tea_name    | department |
+----------+-------------+------------+
| 2-271    | LiMei       | Computer   |
| 3-105    | LiMei       | Computer   |
| 4-321    | MaDi        | Bio        |
| 1-245    | ShaoGuoYing | Math       |
+----------+-------------+------------+

实际的:

mysql> select tea_name,department from teacher
    -> where tea_num in (select tea_num from course);
+-------------+------------+
| tea_name    | department |
+-------------+------------+
| LiMei       | Computer   |
| MaDi        | Bio        |
| ShaoGuoYing | Math       |
+-------------+------------+

2、查询至少有2名男生的班号:(条件加分组查询)

学生表:

mysql> select * from student;
+---------+----------+---------+---------------------+-------+
| stu_num | stu_name | stu_sex | stu_birth           | class |
+---------+----------+---------+---------------------+-------+
| 11215   | JiaWei   | F       | 1993-07-28 00:00:00 | 112   |
| 11328   | DingQi   | F       | 1994-08-15 00:00:00 | 113   |
| 11422   | Baker    | F       | 1999-09-22 00:00:00 | 114   |
| 11423   | Bob      | M       | 1998-04-25 00:00:00 | 114   |
| 11424   | LinJie   | M       | 1994-06-12 00:00:00 | 114   |
| 11425   | XieZhou  | M       | 1995-03-11 00:00:00 | 114   |
| 11426   | MingHui  | F       | 1998-08-09 00:00:00 | 114   |
| 11427   | NanNan   | F       | 1995-10-20 00:00:00 | 114   |
+---------+----------+---------+---------------------+-------+

实际操作:

mysql> select class from student
    -> where stu_sex='M' group by class having count(*)>=2;
+-------+
| class |
+-------+
| 114   |
+-------+

3、查询student表中姓名不以B开头的同学记录:

mysql> select * from student
    -> where stu_name not like 'B%';
+---------+----------+---------+---------------------+-------+
| stu_num | stu_name | stu_sex | stu_birth           | class |
+---------+----------+---------+---------------------+-------+
| 11215   | JiaWei   | F       | 1993-07-28 00:00:00 | 112   |
| 11328   | DingQi   | F       | 1994-08-15 00:00:00 | 113   |
| 11424   | LinJie   | M       | 1994-06-12 00:00:00 | 114   |
| 11425   | XieZhou  | M       | 1995-03-11 00:00:00 | 114   |
| 11426   | MingHui  | F       | 1998-08-09 00:00:00 | 114   |
| 11427   | NanNan   | F       | 1995-10-20 00:00:00 | 114   |
+---------+----------+---------+---------------------+-------+

4、查询student表中每个学生的姓名和年龄:

当前年份:year函数与now函数:

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2020 |
+-------------+

学生出生年份:

mysql> select stu_name,year(stu_birth) from student;
+----------+-----------------+
| stu_name | year(stu_birth) |
+----------+-----------------+
| JiaWei   |            1993 |
| DingQi   |            1994 |
| Baker    |            1999 |
| Bob      |            1998 |
| LinJie   |            1994 |
| XieZhou  |            1995 |
| MingHui  |            1998 |
| NanNan   |            1995 |
+----------+-----------------+

别忘了取个别名:

mysql> select stu_name,year(now())-year(stu_birth) as age from student;
+----------+------+
| stu_name | age  |
+----------+------+
| JiaWei   |   27 |
| DingQi   |   26 |
| Baker    |   21 |
| Bob      |   22 |
| LinJie   |   26 |
| XieZhou  |   25 |
| MingHui  |   22 |
| NanNan   |   25 |
+----------+------+

5、查询student表中最大和最小的birthday日期值:max()与min()

mysql> select max(stu_birth),min(stu_birth) from student;
+---------------------+---------------------+
| max(stu_birth)      | min(stu_birth)      |
+---------------------+---------------------+
| 1999-09-22 00:00:00 | 1993-07-28 00:00:00 |
+---------------------+---------------------+

实际上,max的年份数字大于min的年份数字,但是应该年份越大的年龄越小才对。

mysql> select max(stu_birth) as '最小',min(stu_birth) as '最大' from student;
+---------------------+---------------------+
| 最小                | 最大                |
+---------------------+---------------------+
| 1999-09-22 00:00:00 | 1993-07-28 00:00:00 |
+---------------------+---------------------+

 6、以班号和年龄从大到小的顺序查询student表中的全部记录:

mysql> select * from student order by class desc,stu_birth asc;
+---------+----------+---------+---------------------+-------+
| stu_num | stu_name | stu_sex | stu_birth           | class |
+---------+----------+---------+---------------------+-------+
| 11424   | LinJie   | M       | 1994-06-12 00:00:00 | 114   |
| 11425   | XieZhou  | M       | 1995-03-11 00:00:00 | 114   |
| 11427   | NanNan   | F       | 1995-10-20 00:00:00 | 114   |
| 11423   | Bob      | M       | 1998-04-25 00:00:00 | 114   |
| 11426   | MingHui  | F       | 1998-08-09 00:00:00 | 114   |
| 11422   | Baker    | F       | 1999-09-22 00:00:00 | 114   |
| 11328   | DingQi   | F       | 1994-08-15 00:00:00 | 113   |
| 11215   | JiaWei   | F       | 1993-07-28 00:00:00 | 112   |
+---------+----------+---------+---------------------+-------+

先按照班级排序,再按照年龄排序的。

7、查询最高分同学stu_num、cour_num、degree:

mysql> select * from score
    -> where degree=(select max(degree) from score);
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11422   | 3-105    |     92 |
+---------+----------+--------+

8、查询与Bob同学同性别的所有同学的名字:

mysql> select stu_name from student
    -> where stu_sex=(select stu_sex from student where stu_name='Bob');
+----------+
| stu_name |
+----------+
| Bob      |
| LinJie   |
| XieZhou  |
+----------+

和Bob同班同性别的同学的姓名:

mysql> select stu_name from student
    -> where stu_sex=(select stu_sex from student where stu_name='Bob')
    -> and class=(select class from student where stu_name='Bob');
+----------+
| stu_name |
+----------+
| Bob      |
| LinJie   |
| XieZhou  |
+----------+

9、查询选修数学课程的女同学的成绩表:

女同学学号:

mysql> select stu_num from student
    -> where stu_sex='F';
+---------+
| stu_num |
+---------+
| 11215   |
| 11328   |
| 11422   |
| 11426   |
| 11427   |
+---------+

数学编号:

mysql> select cour_num from course
    -> where cour_name='Math';
+----------+
| cour_num |
+----------+
| 1-245    |
+----------+

成绩表:

mysql> select * from score;
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11422   | 3-105    |     92 |
| 11423   | 1-245    |     84 |
| 11423   | 2-271    |     75 |
| 11424   | 4-321    |     75 |
| 11425   | 2-271    |     89 |
| 11426   | 1-245    |     61 |
| 11426   | 2-271    |     82 |
| 11427   | 1-245    |     78 |
+---------+----------+--------+

筛选:

mysql> select * from score
    -> where stu_num in (select stu_num from student where stu_sex='F')
    -> and cour_num=(select cour_num from course where cour_name='Math');
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11426   | 1-245    |     61 |
| 11427   | 1-245    |     78 |
+---------+----------+--------+

10、查询所有同学的stu_num、cour_num、rank列:

创建一个等级表:

mysql> create table grade(
    -> low int(3),
    -> high int(3),
    -> grade char(1)
    -> );

插入数据后的grade表:

mysql> SELECT * FROM grade;
+------+------+-------+
| low  | high   | grade |
+------+------+-------+
|   90 |  100 | A     |
|   80 |   89 | B     |
|   70 |   79 | C     |
|   60 |   69 | D     |
|    0 |   59 | E     |
+------+------+-------+

成绩表:

mysql> select * from score;
+---------+----------+--------+
| stu_num | cour_num | degree |
+---------+----------+--------+
| 11422   | 3-105    |     92 |
| 11423   | 1-245    |     84 |
| 11423   | 2-271    |     75 |
| 11424   | 4-321    |     75 |
| 11425   | 2-271    |     89 |
| 11426   | 1-245    |     61 |
| 11426   | 2-271    |     82 |
| 11427   | 1-245    |     78 |
+---------+----------+--------+

结果:

mysql> select stu_num,cour_num,grade from score,grade
    -> where degree between low and high;
+---------+----------+-------+
| stu_num | cour_num | grade |
+---------+----------+-------+
| 11422   | 3-105    | A     |
| 11423   | 1-245    | B     |
| 11423   | 2-271    | C     |
| 11424   | 4-321    | C     |
| 11425   | 2-271    | B     |
| 11426   | 1-245    | D     |
| 11426   | 2-271    | B     |
| 11427   | 1-245    | C     |
+---------+----------+-------+

猜你喜欢

转载自www.cnblogs.com/direwolf22/p/12689980.html