查询练习

一、查询练习的准备

准备创建 几个表:

学生表(Student):学号、姓名、性别、出生年月日、班级
课程表(Course):课程号、课程名称、教师编号
成绩表(Score) :学号、课程号、成绩
教师表(Teacher):教师编号、教师性别、教师性别、出生年月日、职称、所在部门

#创建一个test2新数据库
mysql> create database `test2` character set utf8;
Query OK, 1 row affected, 1 warning (0.67 sec)#创建学生表
mysql> create table student(        -> snumber varchar(20) primary key,-> sname varchar(20) not null,-> ssex varchar(20) not null,-> sbirthday datetime,-> class varchar(20)-> );
Query OK, 0 rows affected (1.06 sec)#创建老师表
mysql> create table teacher(-> tnumber varchar(20) primary key,-> tname varchar(20) not null,-> tsex varchar(20) not null,-> tbirthday datetime,-> prof varchar(20) not null,-> depart varchar(20) not null-> );
Query OK, 0 rows affected (0.88 sec)#创建课程表
mysql> create table course(-> cnumber varchar(20) primary key,-> cname varchar(20) not null,-> tnumber varchar(20) not null,#其中tnumber和老师表中的tnumber一样,使用外键-> foreign key(tnumber) references teacher(tnumber) # 老师编号必须来自老师表中的编号-> );
Query OK, 0 rows affected (1.02 sec)#创建成绩表
mysql> create table score(-> snumber varchar(20) not null,-> cnumber varchar(20) not null,-> degree decimal,-> foreign key(snumber) references student(snumber),-> foreign key(cnumber) references course(cnumber),#一个联合主键,学生号和课程号不重复就好-> primary key(snumber,cnumber) -> );
Query OK, 0 rows affected (1.01 sec)

向数据表中添加数据,直接看结果:

mysql> select * from student;
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.00 sec)mysql> select * from teacher;
+---------+--------+------+---------------------+-----------+-----------------+
| tnumber | tname  | tsex | tbirthday           | prof      | depart          |
+---------+--------+------+---------------------+-----------+-----------------+
| 111     | 古一   | 女   | 0000-01-01 00:00:00 | 教授      | 化学系          |
| 112     | 王     | 男   | 2000-09-03 00:00:00 | 副教授    | 计算机系        |
| 113     | 春丽   | 女   | 1988-11-05 00:00:00 | 助教      | 英语系          |
| 114     | 刘邦   | 男   | 1978-12-03 00:00:00 | 助教      | 通信工程系      |
+---------+--------+------+---------------------+-----------+-----------------+
4 rows in set (0.00 sec)mysql> select * from course;
+---------+--------------+---------+
| cnumber | cname        | tnumber |
+---------+--------------+---------+
| 3-105   | 数据结构     | 112     |
| 3-245   | 模拟电路     | 113     |
| 6-166   | 人工智能     | 111     |
| 9-888   | 数字电路     | 114     |
+---------+--------------+---------+
4 rows in set (0.00 sec)mysql> select * from score;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 102     | 3-105   |     83 |
| 103     | 3-105   |     89 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
| 106     | 6-166   |     92 |
+---------+---------+--------+
7 rows in set (0.00 sec)

二、查询练习

查询练习
1、查询student表中的所有记录
2、查询student表中所有记录的sname、ssex、class 列
3、查询教师的所以单位,即不重复的depart列
4、查询score表中成绩 60到90 之间的所有记录
5、查询score表中85、95或83的记录
6、查询student表中班级为一班或性别为女的同学记录
7、按照学号(snumber)升序降序的方式查询student表中的记录
8、按照教师号(cnumber)升序、成绩(degree)降序查询score表中的记录
9、查询一班的人数
10、查询score表中最高分的学生学号和课程号
11、查询每门课的平均成绩
12、查询score表中至少有 两名学生选修 并以 3开头 的课程平均成绩(分组条件与模糊查询)
13、查询成绩大于70,小于90的列
14、查询所有学生的sname、cnumber、degree(多表查询)
15、查询所有学生的snumber、cname、degree(多表查询)
16、查询所有学生的cname、sname,degree(三表关联查询)
17、查询一班学生每门课的平均成绩
18、查询选修‘3-105’课程中成绩高于102号同学成绩的同学记录
19、查询所有课程的成绩高于‘3-105’课程中成绩高于102号同学成绩的同学记录
20、查询学号为100、104 的同学同年出生的所有学生的snumber、sname和sbirthday
21、查询‘古一’老师任课的学生成绩(多层嵌套子查询)
22、查询选修某门课人数多于2人的教师姓名
23、查询一班二班全体学生记录
24 、查询存在85分以上成绩的课程号
25、查询通信工程系教师所教课程的成绩表
26、查询 计算机系 与 化学系 不同职称的教师的tname和prof(职称)

1. 查询student表中的所有记录

其实这个前面已经接触过了
mysql> select * from  student;

mysql> select * from student;
# 其中 * 表示所有字段的意思+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.00 sec)

2. 查询student表中所有记录的sname、ssex、class 列

mysql> select + 要查询的列(多个用逗号隔开) + from + 表名;

mysql> select sname,ssex,class from student;
+-----------+------+--------+
| sname     | ssex | class  |
+-----------+------+--------+
| 张三      | 男   | 一班   |
| 李四      | 男   | 一班   |
| 王二      | 女   | 一班   |
| 王尼玛    | 男   | 一班   |
| 张全蛋    | 男   | 一班   |
| 赵铁柱    | 男   | 二班   |
| 木子      | 女   | 二班   |
+-----------+------+--------+
7 rows in set (0.00 sec)

3、查询教师的所以单位,即不重复的depart列

如果仅仅查询depart 发现有很多重复的

mysql> select depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 化学系          |
| 计算机系        |
| 通信工程系      |
| 通信工程系      |
+-----------------+
4 rows in set (0.00 sec)

发现查询的结果有重复的,可以在select后面加一个distinct(排除重复)
结果就没有重复的单位了
mysql> select distinct depart from teacher;

mysql> select distinct depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 化学系          |
| 计算机系        |
| 通信工程系      |
+-----------------+
3 rows in set (0.10 sec)

4、查询score表中成绩 60到90 之间的所有记录

  • 这就要给我们查询指令加一个查询区间:( between 。。 and 。。)
    mysql> select * from score where degree between 60 and 80;between包括端点值
  • 也可以直接使用运算符比较:
    mysql> select * from score where degree >= 60 and degree<= 90;
    两条指令结果是一样的(注意between包括端点值)
mysql> select * from score where degree between 60 and 90;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 102     | 3-105   |     83 |
| 103     | 3-105   |     89 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
+---------+---------+--------+
5 rows in set (0.00 sec)

5、查询score表中85、95或83的记录

一个字段中的不同值的查询。
要用到表示或者关系的查询 in

mysql> select * from score where degree in (85,95,83);
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 102     | 3-105   |     83 |
+---------+---------+--------+
3 rows in set (0.00 sec)

6、查询student表中班级为一班或性别为女的同学记录

这就和上一个有点区别,这里筛选的是不同字段,上一个是同一个字段当中
or 表示或者:

mysql> select * from student where class='一班' or ssex='女';
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
+---------+-----------+------+---------------------+--------+
6 rows in set (0.00 sec)

筛选出来的要么是一班的要么是女的。

7、按照学号(snumber)升序降序的方式查询student表中的记录

升序asc、降序desc
mysql> select * from student order by snumber(什么字段) desc(降序);
数据库默认排序方式是升序排列的,不输入asc结果相同:
mysql> select * from student order by snumber(什么字段) asc
mysql> select * from student order by snumber; 两种结果相同

mysql> select * from student order by snumber desc;
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.00 sec)

8、按照教师号(cnumber)升序、成绩(degree)降序查询score表中的记录

mysql> select * from score order by cnumber asc ,degree desc;
这条语句会先按照教师号升序排列,遇到相同的教师号再按照成绩降序进行排列
(order by 先按照第一个排,再考虑第二个排列)
当两个同时排序的话,asc(升序)必须要写(sql8.0不写也没事)

ysql> select * from score order by cnumber asc ,degree desc;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 103     | 3-105   |     89 |
| 102     | 3-105   |     83 |
| 101     | 3-245   |     95 |
| 100     | 3-245   |     85 |
| 104     | 3-245   |     66 |
| 106     | 6-166   |     92 |
| 105     | 6-166   |     60 |
+---------+---------+--------+
7 rows in set (0.00 sec)

9、查询一班的人数

统计 count

mysql> select count(*) from student where class='一班';
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.15 sec)

10、查询score表中最高分的学生学号和课程号

mysql> select snumber,cnumber from score where degree=(select max(degree) from score);
这是一个复合语句,是一个子查询,下面会讲到。

mysql> select snumber,cnumber from score where degree=(select max(degree) from score);
+---------+---------+
| snumber | cnumber |
+---------+---------+
| 101     | 3-245   |
+---------+---------+
1 row in set (0.35 sec)

(1)找到最高分
select max(degree) from score
(2)找到最高分的学号和课程号
mysql> select snumber,cnumber from score where degree=(select max(degree) from score);

排序的做法:
mysql> select snumber,cnumber,degree from score order by degree desc limit 0,1;
这里的limit 0,1表示取表中从第0条取到第一条(也就是取出第一条数据)
如果有两个最高分,会有一点小问题的。
limit 的第一个数字表示从哪里开始查,第二个数字表示查几条

mysql> select snumber,cnumber,degree from score order by degree desc  limit 0,1;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 101     | 3-245   |     95 |
+---------+---------+--------+
1 row in set (0.00 sec)

11. 查询每门课的平均成绩

mysql> select * from course;
+---------+--------------+---------+
| cnumber | cname        | tnumber |
+---------+--------------+---------+
| 3-105   | 数据结构     | 112     |
| 3-245   | 模拟电路     | 113     |
| 6-166   | 人工智能     | 111     |
| 9-888   | 数字电路     | 114     |
+---------+--------------+---------+
4 rows in set (0.00 sec)
#先看一下这门课的学生所有成绩
mysql> select degree from score where cnumber='3-105';
+--------+
| degree |
+--------+
|     83 |
|     89 |
+--------+
2 rows in set (0.11 sec)#计算平均成绩
mysql> select avg(degree) from score where cnumber='3-105';
+-------------+
| avg(degree) |
+-------------+
|     86.0000 |
+-------------+
1 row in set (0.02 sec)

但是我们这只计算了一门,怎么计算每一门呢?一条一条语句的写是可以的,但是比较麻烦,下面写在一条语句中:
用到了 group by 先把课程号分组再进行计算。
“Group By” 从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理

mysql> select cnumber,avg(degree) from score group by cnumber;
+---------+-------------+
| cnumber | avg(degree) |
+---------+-------------+
| 3-105   |     86.0000 |
| 3-245   |     82.0000 |
| 6-166   |     76.0000 |
+---------+-------------+
3 rows in set (0.00 sec)

12、查询score表中至少有 两名学生选修 并以 3开头 的课程平均成绩(分组条件与模糊查询)

这个题看着有点难,我们先看一下score表,再分一下组

mysql> select * from score;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 102     | 3-105   |     83 |
| 103     | 3-105   |     89 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
| 106     | 6-166   |     92 |
+---------+---------+--------+
7 rows in set (0.01 sec)mysql> select cnumber from score group by cnumber;
+---------+
| cnumber |
+---------+
| 3-105   |
| 3-245   |
| 6-166   |
+---------+
3 rows in set (0.10 sec)

这时候给分组加上条件 having + 条件,就是分组后跟条件要使用having

mysql> select cnumber from score ->  group by cnumber  #分组-> having count(cnumber)>=2; #条件
+---------+
| cnumber |
+---------+
| 3-105   |
| 3-245   |
| 6-166   |
+---------+
3 rows in set (0.00 sec)mysql> select cnumber from score group by cnumber-> having count(cnumber)>=4;#条件
Empty set (0.00 sec)

还有一个条件,就是以3开头,这里可以用 模糊查询(使用 like )

mysql> select cnumber from score group by cnumber-> having count(cnumber)>=2 and cnumber like '3%'; #3%表示以3开头, %为3后面的任意匹配
+---------+
| cnumber |
+---------+
| 3-105   |
| 3-245   |
+---------+
2 rows in set (0.35 sec)

再加上计算平均值和这门课的人数

mysql> select cnumber,avg(degree),count(*) from score group by cnumber-> having count(cnumber)>=2 and cnumber like '3%';
+---------+-------------+----------+
| cnumber | avg(degree) | count(*) |
+---------+-------------+----------+
| 3-105   |     86.0000 |        2 |
| 3-245   |     82.0000 |        3 |
+---------+-------------+----------+
2 rows in set (0.00 sec)

13、查询成绩大于70,小于90的列

mysql> select snumber,degree from score-> where degree>70 and degree<90;
+---------+--------+
| snumber | degree |
+---------+--------+
| 100     |     85 |
| 102     |     83 |
| 103     |     89 |
+---------+--------+
3 rows in set (0.00 sec)
# select后面的内容是要打印的内容

当然也可以用between

mysql> select snumber,degree from score-> where degree between 70 and 90;

这两种方法可以相互替换实现范围查询,但是要注意between是包含端点的…

14、查询所有学生的sname、cnumber、degree(多表查询)

下面使用多表查询:

mysql> select snumber,cnumber,degree from score;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 102     | 3-105   |     83 |
| 103     | 3-105   |     89 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
| 106     | 6-166   |     92 |
+---------+---------+--------+
7 rows in set (0.00 sec)mysql> select snumber,sname from student;
+---------+-----------+
| snumber | sname     |
+---------+-----------+
| 100     | 张三      |
| 101     | 李四      |
| 102     | 王二      |
| 103     | 王尼玛    |
| 104     | 张全蛋    |
| 105     | 赵铁柱    |
| 106     | 木子      |
+---------+-----------+
7 rows in set (0.00 sec)

多表查询:
显然我们想把score表中snumber替换成对应的姓名sname
mysql> select sname,cnumber,degree from student,score where student.snumber=score.snumber;#加上限制条件,不然会乱

mysql> select sname,cnumber,degree from student,score-> where student.snumber=score.snumber;  #加上限制条件,不然会乱
+-----------+---------+--------+
| sname     | cnumber | degree |
+-----------+---------+--------+
| 张三      | 3-245   |     85 |
| 李四      | 3-245   |     95 |
| 王二      | 3-105   |     83 |
| 王尼玛    | 3-105   |     89 |
| 张全蛋    | 3-245   |     66 |
| 赵铁柱    | 6-166   |     60 |
| 木子      | 6-166   |     92 |
+-----------+---------+--------+
7 rows in set (0.00 sec)

15、查询所有学生的snumber、cname、degree(多表查询)

cnumber是一样的,通过这个来写条件:

mysql> select cname,snumber,degree from score,course-> where score.cnumber=course.cnumber;
+--------------+---------+--------+
| cname        | snumber | degree |
+--------------+---------+--------+
| 模拟电路     | 100     |     85 |
| 模拟电路     | 101     |     95 |
| 数据结构     | 102     |     83 |
| 数据结构     | 103     |     89 |
| 模拟电路     | 104     |     66 |
| 人工智能     | 105     |     60 |
| 人工智能     | 106     |     92 |
+--------------+---------+--------+
7 rows in set (0.00 sec)

16、查询所有学生的cname、sname,degree(三表关联查询)

这里的 sname来自student表、cname来自course表,degree来自score表,也就是我们要查询的三个字段来自三张表
找两两之间的相同之处,写条件

mysql> select sname,cname,degree from student,course,score #三个数据来自三个表where student.snumber=score.snumber  #利用score表中的重复字段来查询and course.cnumber=score.cnumber;
+-----------+--------------+--------+
| sname     | cname        | degree |
+-----------+--------------+--------+
| 张三      | 模拟电路     |     85 |
| 李四      | 模拟电路     |     95 |
| 王二      | 数据结构     |     83 |
| 王尼玛    | 数据结构     |     89 |
| 张全蛋    | 模拟电路     |     66 |
| 赵铁柱    | 人工智能     |     60 |
| 木子      | 人工智能     |     92 |
+-----------+--------------+--------+
7 rows in set (0.00 sec)

这里再查询下cnumber和snumber

mysql> select sname,cname,degree,student.snumber,course.cnumber from student,course,score-> where student.snumber=score.snumber and course.cnumber=score.cnumber;
+-----------+--------------+--------+---------+---------+
| sname     | cname        | degree | snumber | cnumber |
+-----------+--------------+--------+---------+---------+
| 张三      | 模拟电路     |     85 | 100     | 3-245   |
| 李四      | 模拟电路     |     95 | 101     | 3-245   |
| 王二      | 数据结构     |     83 | 102     | 3-105   |
| 王尼玛    | 数据结构     |     89 | 103     | 3-105   |
| 张全蛋    | 模拟电路     |     66 | 104     | 3-245   |
| 赵铁柱    | 人工智能     |     60 | 105     | 6-166   |
| 木子      | 人工智能     |     92 | 106     | 6-166   |
+-----------+--------------+--------+---------+---------+
7 rows in set (0.00 sec)

注意这里要查询的snumber和cnumber都加了条件,因为这两个数据出现在多个表中,如果不指定电脑不知道找哪一个表中的(尽管都相同),但是会报错
ERROR 1052 (23000): Column 'snumber' in field list is ambiguous

17、查询一班学生每门课的平均成绩

一个字段中的不同值的查询。
要用到表示或者关系的查询 in
select snumber from student where class='一班' 查询student表中所有班级是一班的学号snumber,相当于在(一班同学的snumber)
然后在score表中查询所有学号snumber在一班集合里的所有人的成绩,
select avg(degree) from score where snumber in (select snumber from student where class='一班');

mysql> select avg(degree) from scorewhere snumber in (select snumber from student where class='一班');
+-------------+
| avg(degree) |
+-------------+
|     83.6000 |
+-------------+
1 row in set (0.18 sec)

但是这是所有课程的平均成绩,我们要求的是每门课的平均成绩,其实就是按照老师号进行分组即可 group by cnumber

mysql> select cnumber, avg(degree) from score where snumber in (select snumber from student where class='一班')-> group by cnumber;  #按照老师号进行分组
+---------+-------------+
| cnumber | avg(degree) |
+---------+-------------+
| 3-245   |     82.0000 |
| 3-105   |     86.0000 |
+---------+-------------+
2 rows in set (0.16 sec)

18、查询选修‘3-105’课程中成绩高于102号同学成绩的同学记录

先把102号同学的3-105课程的成绩导出来,用到了and(同时)

mysql> select degree from score where snumber='102' and cnumber='3-105';
+--------+
| degree |
+--------+
|     83 |
+--------+
1 row in set (0.00 sec)

有了这个条件,我们再加一个3-105课程就可以筛选出来了

mysql> select snumber,degree from score where#成绩条件-> degree>(select degree from score where snumber='102' and cnumber='3-105')   #课程号条件-> and cnumber='3-105';
+---------+--------+
| snumber | degree |
+---------+--------+
| 103     |     89 |
+---------+--------+
1 row in set (0.03 sec)

19、查询所有课程的成绩高于‘3-105’课程中成绩高于102号同学成绩的同学记录

这题把~和上面那一题几乎类似,只是条件不同而已,把从3-105课程中查询大于102号同学成绩,放大到从整个表中查询比 102号同学的3-105课程成绩多的同学记录!
我们只要把上题的条件删除一个就好:

mysql> select snumber,degree from score where-> degree>(select degree from score where snumber='102' and cnumber='3-105');
+---------+--------+
| snumber | degree |
+---------+--------+
| 100     |     85 |
| 101     |     95 |
| 103     |     89 |
| 106     |     92 |
+---------+--------+
4 rows in set (0.00 sec)

20、查询学号为100、104 的同学同年出生的所有学生的snumber、sname和sbirthday

先查出这两个同学的年份

mysql> select year(sbirthday) from student where snumber in (100,104);
+-----------------+
| year(sbirthday) |
+-----------------+
|            1999 |
|            2000 |
+-----------------+
2 rows in set (0.04 sec)

有了年份,就可以进行筛选了,注意这里不能用 = 来做条件因为这里的年份是两个值,应该用 in,有一个条件用 =,两个以上条件用 in

mysql> select snumber,sname,sbirthday from student-> where year(sbirthday) in (select year(sbirthday) from student where snumber in (100,104));
+---------+-----------+---------------------+
| snumber | sname     | sbirthday           |
+---------+-----------+---------------------+
| 100     | 张三      | 1999-09-01 00:00:00 |
| 101     | 李四      | 1999-02-11 00:00:00 |
| 102     | 王二      | 1999-09-23 00:00:00 |
| 104     | 张全蛋    | 2000-09-03 00:00:00 |
| 106     | 木子      | 2000-12-16 00:00:00 |
+---------+-----------+---------------------+
5 rows in set (0.04 sec)

这就找到了,这里用到了内置函数year()求年份
已经知道的函数 avg()求平均值,count()求和

21、查询‘古一’老师任课的学生成绩(多层嵌套子查询)

我们先看一下古一的个人信息:

mysql> select * from teacher where tname='古一';
+---------+--------+------+---------------------+--------+-----------+
| tnumber | tname  | tsex | tbirthday           | prof   | depart    |
+---------+--------+------+---------------------+--------+-----------+
| 111     | 古一   | 女   | 0000-01-01 00:00:00 | 教授   | 化学系    |
+---------+--------+------+---------------------+--------+-----------+
1 row in set (0.00 sec)

我们要从这个表中得到古一的tnumber,再根据这个tnumner在course表中找到她教的课程的cnumber号:

mysql> select cnumber from course > where tnumber=(select tnumber from teacher where tname='古一');
+---------+
| cnumber |
+---------+
| 6-166   |
+---------+
1 row in set (0.00 sec)

知道了cnumber号就可以从score表中得到她教这门课的平均成绩了

mysql> select avg(degree) from score-> where cnumber=( select cnumber from course -> where tnumber=(select tnumber from teacher where tname='古一') );
+-------------+
| avg(degree) |
+-------------+
|     76.0000 |
+-------------+
1 row in set (0.01 sec)

22、查询选修某门课人数多于2人的教师姓名

先查询人数多于2人的课程号,再查询老师的tnumber,再查询老师的姓名,步步嵌套

mysql> select tname from teacher#条件3:以条件2查找老师的名字-> where tnumber = #条件2:以条件1位条件找到 老师的tnumber,->(select tnumber from course  where cnumber=#条件1:人数多于2人的课程号-> ( select cnumber from score  group by cnumber having count(*)>2 ) );
+--------+
| tname  |
+--------+
| 春丽   |
+--------+
1 row in set (0.00 sec)

23、查询一班二班全体学生记录

mysql> select * from student where class in ('一班','二班');
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.25 sec)

24 、查询存在85分以上成绩的课程号

很简单这个,因为要查的数据都在一个表里(score)

mysql> select * from score;
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 102     | 3-105   |     83 |
| 103     | 3-105   |     89 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
| 106     | 6-166   |     92 |
+---------+---------+--------+
7 rows in set (0.05 sec)mysql> select cnumber from score where degree >85;
+---------+
| cnumber |
+---------+
| 3-245   |
| 3-105   |
| 6-166   |
+---------+
3 rows in set (0.12 sec)

25、查询通信工程系教师所教课程的成绩表

先在teacher表中找到通信工程系的老师的编号,然后score表中没有老师编号,只有课程编号,发现课程表中有老师编号和课程编号。所以要在课程表中根据老师编号找到课程编号,然后在score表中根据课程编号找到成绩。

mysql> select * from score where cnumber in (select cnumber from course where tnumber in (select tnumber from teacher where depart='通信工程系') );
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 100     | 3-245   |     85 |
| 101     | 3-245   |     95 |
| 104     | 3-245   |     66 |
+---------+---------+--------+
3 rows in set (0.00 sec)

26、查询 计算机系 与 化学系 不同职称的教师的tname和prof(职称)

题目的意思就是查询计算机系与化学系中职称不相同的老师
用到not in
select prof from teacher where depart='通信工程系'表示通信工程系老师的职称都有哪些。
prof not in(select prof from teacher where depart='通信工程系')表示职称不在上面那几个职称中的职称

mysql> select * from teacher where depart='计算机系' and prof not in(select prof from teacher where depart='通信工程系');
+---------+-------+------+---------------------+-----------+--------------+
| tnumber | tname | tsex | tbirthday           | prof      | depart       |
+---------+-------+------+---------------------+-----------+--------------+
| 112     | 王    | 男   | 2000-09-03 00:00:00 | 副教授    | 计算机系     |
+---------+-------+------+---------------------+-----------+--------------+
1 row in set (0.51 sec)

再反过来查通信工程系中与计算机系中不重复的

mysql> select * from teacher where depart='通信工程系' and prof not in(select prof from teacher where depart='计算机系');
+---------+--------+------+---------------------+--------+-----------------+
| tnumber | tname  | tsex | tbirthday           | prof   | depart          |
+---------+--------+------+---------------------+--------+-----------------+
| 113     | 春丽   | 女   | 1988-11-05 00:00:00 | 助教   | 通信工程系      |
| 114     | 刘邦   | 男   | 1978-12-03 00:00:00 | 助教   | 通信工程系      |
+---------+--------+------+---------------------+--------+-----------------+
2 rows in set (0.00 sec)

这两个语句可以通过union连接在一起

mysql> select * from teacher where depart='计算机系' and prof not in(select prof from teacher where depart='通信工程系')-> union   #连接在一起-> select * from teacher where depart='通信工程系' and prof not in(select prof from teacher where depart='计算机系');
+---------+--------+------+---------------------+-----------+-----------------+
| tnumber | tname  | tsex | tbirthday           | prof      | depart          |
+---------+--------+------+---------------------+-----------+-----------------+
| 112     | 王     | 男   | 2000-09-03 00:00:00 | 副教授    | 计算机系        |
| 113     | 春丽   | 女   | 1988-11-05 00:00:00 | 助教      | 通信工程系      |
| 114     | 刘邦   | 男   | 1978-12-03 00:00:00 | 助教      | 通信工程系      |
+---------+--------+------+---------------------+-----------+-----------------+
3 rows in set (0.38 sec)

27、查询 编号为‘3-105’课程且成绩至少高于编号为‘3-245’的成绩,这些人的cnumber,snumber和degree,并且按照degree的大小进行由高到低的排序(any)

重点就是 至少 :大于其中至少一个,这就用到了 any

mysql> select * from score-> where cnumber='3-105' #条件一-> and degree>any(select degree from score where cnumber = '3-245')#条件二-> order by degree desc;  #排序
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 103     | 3-105   |     89 |
| 102     | 3-105   |     83 |
+---------+---------+--------+
2 rows in set (0.39 sec)

至少 = any
any表示大于括号中的一个即可

28、查询编号为‘3-105’的课程且成绩高于课程‘3-245’课程的同学的信息?(all)

这道题和上一道题相比,少了排序还少了至少,根据题目意思是高于课程‘3-245’所有人成绩的意思.

 mysql> select * from score-> where cnumber='3-105' #条件一-> and degree>all(select degree from score where cnumber = '3-245');

all表示大于括号里的所有。

29、查询所有教师和同学的name、sex和birthday(union、as)

这个前面学到个,可以用 union 去把两个语句连接在一起

mysql> select tname,tsex,tbirthday from teacher-> union-> select sname,ssex,sbirthday from student;
+-----------+------+---------------------+
| tname     | tsex | tbirthday           |
+-----------+------+---------------------+
| 古一      | 女   | 0000-01-01 00:00:00 |
| 王        | 男   | 2000-09-03 00:00:00 |
| 春丽      | 女   | 1988-11-05 00:00:00 |
| 刘邦      | 男   | 1978-12-03 00:00:00 |
| 张三      | 男   | 1999-09-01 00:00:00 |
| 李四      | 男   | 1999-02-11 00:00:00 |
| 王二      | 女   | 1999-09-23 00:00:00 |
| 王尼玛    | 男   | 1988-01-11 00:00:00 |
| 张全蛋    | 男   | 2000-09-03 00:00:00 |
| 赵铁柱    | 男   | 1983-04-05 00:00:00 |
| 木子      | 女   | 2000-12-16 00:00:00 |
+-----------+------+---------------------+
11 rows in set (0.06 sec)

但是会发现,上面的字段不对是tname,这里就要取 别名 …as…

mysql> select tname as name,tsex as sex,tbirthday as birthday from teacher-> union-> select sname,ssex,sbirthday from student;
+-----------+-----+---------------------+
| name      | sex | birthday            |
+-----------+-----+---------------------+
| 古一      | 女  | 0000-01-01 00:00:00 |
| 王        | 男  | 2000-09-03 00:00:00 |
| 春丽      | 女  | 1988-11-05 00:00:00 |
| 刘邦      | 男  | 1978-12-03 00:00:00 |
| 张三      | 男  | 1999-09-01 00:00:00 |
| 李四      | 男  | 1999-02-11 00:00:00 |
| 王二      | 女  | 1999-09-23 00:00:00 |
| 王尼玛    | 男  | 1988-01-11 00:00:00 |
| 张全蛋    | 男  | 2000-09-03 00:00:00 |
| 赵铁柱    | 男  | 1983-04-05 00:00:00 |
| 木子      | 女  | 2000-12-16 00:00:00 |
+-----------+-----+---------------------+
11 rows in set (0.00 sec)
# 第二排可以不用取别名,默认按第一排取别名。

30 、查询所有女教师和女同学的name、sex和birthday

在两个查询语句后面加上where条件就可以,在用union连接
where tsex='女'+where ssex=‘女’

mysql> select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女' -> union -> select sname,ssex,sbirthday from student where ssex='女';

31、查询成绩比课程平均成绩低的同学的成绩表

先查一下各门课的平均成绩

mysql> select avg(degree) from score group by cnumber;
+-------------+
| avg(degree) |
+-------------+
|     86.0000 |
|     82.0000 |
|     76.0000 |
+-------------+
3 rows in set (0.56 sec)

select avg(degree) from score b where a.cnumber=b.cnumber表示:a和b是score的两个复制的表,这行代码意思是,a表中选择3-105然后去和b表中3-105课程的平均值去比较,所以得计算的是a.cnumber=b.cnumber的平均成绩。

mysql> select * from score a where-> degree<(select avg(degree) from score b where a.cnumber=b.cnumber);
+---------+---------+--------+
| snumber | cnumber | degree |
+---------+---------+--------+
| 102     | 3-105   |     83 |
| 104     | 3-245   |     66 |
| 105     | 6-166   |     60 |
+---------+---------+--------+
3 rows in set (0.40 sec)

其中的select avg(degree) from score b where a.cnumber=b.cnumber是当选中a表中的某个同学时,从b表中找到与它课程号相同的同学,然后利用这个同学的课程号求这门课平均成绩,再和a表的那个同学做比较,最后得出结果。

32、查询所有任课老师的tname和depart

这一题乍一看很简单,其实有点小条件,就是任课老师的名字。因为老师表里面可能有不任课的老师,所以要和course表进行比较,找到teccher表中教师号在课程表中存在的任课老师:
所以需要先查课程表中老师的编号,证明这个老师任课。

mysql> select tname,depart from teacher-> where tnumber in (select tnumber from course);
+--------+-----------------+
| tname  | depart          |
+--------+-----------------+
| 古一   | 化学系          |
| 王     | 计算机系        |
| 春丽   | 通信工程系      |
| 刘邦   | 通信工程系      |
+--------+-----------------+
4 rows in set (0.57 sec)

33、查询至少有2个男生的班号

看一下学生表:

mysql> select * from student;
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.00 sec)
mysql> select class from student where ssex='男' group by class having count(*)>=2;
+--------+
| class  |
+--------+
| 一班   |
+--------+
1 row in set (0.00 sec)

34、查询student表中不姓 ‘王’ 的同学记录

这里可以用到模糊查询,并且区分 …not like…
mysql> select * from student where sname not like '王%';

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

年龄=当前年份 - 出生年份,当前年份可以用 year(now())来体现,再加上别名,具体语句如下:

mysql> select sname,year(now())-year(sbirthday) as old from student;
+-----------+------+
| sname     | old  |
+-----------+------+
| 张三      |   20 |
| 李四      |   20 |
| 王二      |   20 |
| 王尼玛    |   31 |
| 张全蛋    |   19 |
| 赵铁柱    |   36 |
| 木子      |   19 |
+-----------+------+
7 rows in set (0.00 sec)

36、查询student表中最大最小sbirthday的日期值

查询所有出生日期,系统默认使用升序排列,可不写、要是想显示降序可以在语句的最后加上asc

mysql> select sbirthday from student order by sbirthday;
+---------------------+
| sbirthday           |
+---------------------+
| 1983-04-05 00:00:00 |
| 1988-01-11 00:00:00 |
| 1999-02-11 00:00:00 |
| 1999-09-01 00:00:00 |
| 1999-09-23 00:00:00 |
| 2000-09-03 00:00:00 |
| 2000-12-16 00:00:00 |
+---------------------+
7 rows in set (0.01 sec)

这里可以使用max()、min()函数

mysql> select max(sbirthday) as max,min(sbirthday) as minfrom student order by sbirthday;
+---------------------+---------------------+
| max                 | min                 |
+---------------------+---------------------+
| 2000-12-16 00:00:00 | 1983-04-05 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)

37、以班级和年龄从大到小的顺序查询student表中的记录

这里order by是先按照第一进行排列,第一个相同再按照第二个进行排列

mysql> select * from student order by class desc,sbirthday;
+---------+-----------+------+---------------------+--------+
| snumber | sname     | ssex | sbirthday           | class  |
+---------+-----------+------+---------------------+--------+
| 105     | 赵铁柱    | 男   | 1983-04-05 00:00:00 | 二班   |
| 106     | 木子      | 女   | 2000-12-16 00:00:00 | 二班   |
| 103     | 王尼玛    | 男   | 1988-01-11 00:00:00 | 一班   |
| 101     | 李四      | 男   | 1999-02-11 00:00:00 | 一班   |
| 100     | 张三      | 男   | 1999-09-01 00:00:00 | 一班   |
| 102     | 王二      | 女   | 1999-09-23 00:00:00 | 一班   |
| 104     | 张全蛋    | 男   | 2000-09-03 00:00:00 | 一班   |
+---------+-----------+------+---------------------+--------+
7 rows in set (0.00 sec)

38、查询男教师以及所上的课程

mysql> select * from teacher where tsex='男';
+---------+--------+------+---------------------+-----------+-----------------+
| tnumber | tname  | tsex | tbirthday           | prof      | depart          |
+---------+--------+------+---------------------+-----------+-----------------+
| 112     | 王     | 男   | 2000-09-03 00:00:00 | 副教授    | 计算机系        |
| 114     | 刘邦   | 男   | 1978-12-03 00:00:00 | 助教      | 通信工程系      |
+---------+--------+------+---------------------+-----------+-----------------+
2 rows in set (0.00 sec)mysql> select * from course where tnumber in ( select tnumber from teacher where tsex='男');
+---------+--------------+---------+
| cnumber | cname        | tnumber |
+---------+--------------+---------+
| 3-105   | 数据结构     | 112     |
| 9-888   | 数字电路     | 114     |
+---------+--------------+---------+
2 rows in set (0.00 sec)

39、查询最高分同学的信息

先找最高分,由最高分找学号,再由学号找信息

mysql> select * from student where-> snumber=(select snumber from score wheredegree=( select max(degree) from score) );
+---------+--------+------+---------------------+--------+
| snumber | sname  | ssex | sbirthday           | class  |
+---------+--------+------+---------------------+--------+
| 101     | 李四   | 男   | 1999-02-11 00:00:00 | 一班   |
+---------+--------+------+---------------------+--------+
1 row in set (0.00 sec)

40、查询和王尼玛同性别的同学名字

mysql> select sname from student where ssex=(select ssex from student where sname='王尼玛');
+-----------+
| sname     |
+-----------+
| 张三      |
| 李四      |
| 王尼玛    |
| 张全蛋    |
| 赵铁柱    |
+-----------+
5 rows in set (0.00 sec)

41、查询和王尼玛同性别且同班的同学名字

只需要用and再给上一题添加一个条件就OK了

mysql> select sname from student where ssex=(select ssex from student where sname='王尼玛')    -> and class=(select class from student where sname='王尼玛');
+-----------+
| sname     |
+-----------+
| 张三      |
| 李四      |
| 王尼玛    |
| 张全蛋    |
+-----------+
4 rows in set (0.00 sec)

42、查询所有选修‘人工智能’课程的男同学的成绩

先从mysql> select cnumber from course where cname='人工智能';找到cnumber
再从mysql> select snumber from student where ssex='男';找到snumber

mysql> select degree from score-> where cnumber=(select cnumber from course where cname='人工智能')-> and snumber in (select snumber from student where ssex='男');
+--------+
| degree |
+--------+
|     60 |
+--------+
1 row in set (0.00 sec)

43、使用如下命令建立一个grade表

建立了一个等级表

mysql> create table grade(-> low int(3),-> upp int(3),-> grade char(1)-> );
Query OK, 0 rows affected (1.58 sec)insert into grade values(90,100,'A');insert into grade values(80,89,'B');insert into grade values(70,79,'C');insert into grade values(60,69,'D');insert into grade values(0,59,'E');mysql> select * from grade;
+------+------+-------+
| low  | upp  | grade |
+------+------+-------+
|   90 |  100 | A     |
|   80 |   89 | B     |
|   70 |   79 | C     |
|   60 |   69 | D     |
|    0 |   59 | E     |
+------+------+-------+
5 rows in set (0.00 sec)

现在查询所有同学的snumber、cnumber、和grade列

mysql> select snumber,cnumber,grade from score,grade-> where degree between low and upp order by grade;  #再排个序
+---------+---------+-------+
| snumber | cnumber | grade |
+---------+---------+-------+
| 106     | 6-166   | A     |
| 101     | 3-245   | A     |
| 100     | 3-245   | B     |
| 102     | 3-105   | B     |
| 103     | 3-105   | B     |
| 105     | 6-166   | D     |
| 104     | 3-245   | D     |
+---------+---------+-------+
7 rows in set (0.00 sec)

MySQL学习笔记数据库学习【二】相关推荐

  1. VSTO 学习笔记(十二)自定义公式与Ribbon

    原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...

  2. tensorflow学习笔记(三十二):conv2d_transpose (解卷积)

    tensorflow学习笔记(三十二):conv2d_transpose ("解卷积") deconv解卷积,实际是叫做conv_transpose, conv_transpose ...

  3. BizTalk学习笔记系列之二:实例说明如何使用BizTalk

    BizTalk学习笔记系列之二:实例说明如何使用BizTalk --.BizTalk学习笔记系列之二<?XML:NAMESPACE PREFIX = O /> Aaron.Gao,2006 ...

  4. Windows保护模式学习笔记(十二)—— 控制寄存器

    Windows保护模式学习笔记(十二)-- 控制寄存器 控制寄存器 Cr0寄存器 Cr2寄存器 Cr4寄存器 控制寄存器 描述: 控制寄存器有五个,分别是:Cr0 Cr1 Cr2 Cr3 Cr4 Cr ...

  5. 汇编入门学习笔记 (十二)—— int指令、port

    疯狂的暑假学习之  汇编入门学习笔记 (十二)--  int指令.port 參考: <汇编语言> 王爽 第13.14章 一.int指令 1. int指令引发的中断 int n指令,相当于引 ...

  6. OpenCV学习笔记(十二):边缘检测:Canny(),Sobel(),Laplace(),Scharr滤波器

    OpenCV学习笔记(十二):边缘检测:Canny(),Sobel(),Laplace(),Scharr滤波器 1)滤波:边缘检测的算法主要是基于图像强度的一阶和二阶导数,但导数通常对噪声很敏感,因此 ...

  7. QT学习笔记(十二):透明窗体设置

    QT学习笔记(十二):透明窗体设置 创建 My_Widget 类 基类为QWidget , My_Widget.cpp 源文件中添加代码 #include "widget.h" # ...

  8. Python基础学习笔记之(二)

    Python基础学习笔记之(二) zouxy09@qq.com http://blog.csdn.net/zouxy09 六.包与模块 1.模块module Python中每一个.py脚本定义一个模块 ...

  9. Kinect开发学习笔记之(二)Kinect开发学习资源整理

    Kinect开发学习笔记之(二)Kinect开发学习资源整理 zouxy09@qq.com http://blog.csdn.net/zouxy09 刚刚接触Kinect,在网上狂搜资料,获得了很多有 ...

最新文章

  1. HTTP1.1协议请求方面参数
  2. oracle重载操作符的例子
  3. 用户操作拦截并作日志记录--自定义注解+AOP拦截
  4. 获取radio和select的值,获取select的值
  5. [转载]国外物联网平台初探(二):微软Azure IoT
  6. CG CTF WEB bypass again
  7. java dump命令例子,常用命令示例
  8. ubuntu18.04安装windows版本微信
  9. NYOJ-----最少乘法次数
  10. 采用jqueryUI创建日期选择器
  11. iOS中UINavigationController控制器使用详解
  12. 如何在 Mac 上设置自定义锁屏信息?
  13. openwrt php wifi 钓鱼,Openwrt自动扫描WiFi并连接中继
  14. 永远不要忽视 粉红色/红色的异样字体 在你不知道为什么跟你期望偏差那么大的时候,,不要急记得去问问为什么
  15. 一维到三维的推广(1D and 3D generalizations of models)
  16. SPSS多元线性回归输出结果的详细解释
  17. 通过查看Qt助手(QtAssistant)了解并使用自己需要Qt库
  18. 本地地址映射成外网地址工具-ngrok
  19. 如何使用串口调试工具测试HX530串口RFID读卡器读取IC卡卡号
  20. 探索推荐引擎内部的秘密系列

热门文章

  1. 【玩转腾讯云】免费搭建你的微信机器人!24小时在线!上云!
  2. mysql常考50_要想学好SQL语句必会的50题,也是考试常考的点
  3. oracle x kglob,x$kglob/x$kgltr 中library cache信息查看(结果同dump内容)
  4. 体验godaddy域名转入,添加A记录,及使用dnspod的NS
  5. 国产自研芯片取得的进展,连外媒都认可了,ARM真怕了
  6. 面试官:递归是个什么东东?
  7. Zookeeper 3.5.7学习记录(一)——集群的坑
  8. nyoj 一笔画问题
  9. Linux作业 使用make命令和分析makefile文件
  10. python 抽奖器_如何基于python实现年会抽奖工具