0
点赞
收藏
分享

微信扫一扫

MySQL多表查询作业

unadlib 2022-04-21 阅读 17
mysql

MySQL多表查询作业

素材

/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;		//检测是否存在表`student`,存在删掉,不存在不执行

CREATE TABLE student (
id INT(10) NOT NULL UNIQUE  PRIMARY KEY  ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4)  ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50) 
);

/*Data for the table `student` */
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

/*Table structure for table `score` */
DROP TABLE IF EXISTS `score`;		//检测是否存在表`score`,存在删掉,不存在不执行

CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);

/*Data for the table `score` */
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

查询要求

1. 查询student表的所有记录

mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)

2. 查询student表的第2条到4条记录

mysql> select * from student limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二    ||  1986 | 中文系     | 北京市昌平区       |
| 903 | 张三      ||  1990 | 中文系     | 湖南省永州市       |
| 904 | 李四      ||  1990 | 英语系     | 辽宁省阜新市       |
+-----+-----------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
#MySQL序号从0开始,因此第二条记录序号为1,第四条记录序号为3
#“limit”关键字表示限制显示行数

3. 从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

mysql> select id 学号,name 姓名,department 院系 from student;
+--------+-----------+--------------+
| 学号   | 姓名      | 院系         |
+--------+-----------+--------------+
|    901 | 张老大    | 计算机系     |
|    902 | 张老二    | 中文系       |
|    903 | 张三      | 中文系       |
|    904 | 李四      | 英语系       |
|    905 | 王五      | 英语系       |
|    906 | 王六      | 计算机系     |
+--------+-----------+--------------+
6 rows in set (0.00 sec)

4. 从student表中查询计算机系和英语系的学生的信息

mysql> select * from student where (department='计算机系') or (department='英语系');
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)
/*或者*/
mysql> select * from student where department in ('计算机系','英语系');
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)
#两种方法都可以

5. 从student表中查询年龄28-32岁的学生信息

mysql> select * from student where (year(now())-birth) between 28 and 32;
+-----+--------+------+-------+------------+--------------------+
| id  | name   | sex  | birth | department | address            |
+-----+--------+------+-------+------------+--------------------+
| 903 | 张三   ||  1990 | 中文系     | 湖南省永州市       |
| 904 | 李四   ||  1990 | 英语系     | 辽宁省阜新市       |
| 905 | 王五   ||  1991 | 英语系     | 福建省厦门市       |
+-----+--------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
#“year()”函数表示输出年份,括号内参数需要指定,“now()”函数表示输出当前时间,两者结合表示输出当前年份
#使用(year(now())-s.birth)可求出每个学生对应的年龄

6. 从student表中查询每个院系有多少人

mysql> select department 院系,count(*) 人数 from student group by department;
+--------------+--------+
| 院系         | 人数   |
+--------------+--------+
| 中文系       |      2 |
| 英语系       |      2 |
| 计算机系     |      2 |
+--------------+--------+
3 rows in set (0.00 sec)
#“count(*)”关键字表示统计
#“group by department”表示按院系进行聚合运算

7. 从score表中查询每个科目的最高分

mysql> select c_name 科目,max(grade) 最高分 from score group by c_name;
+-----------+-----------+
| 科目      | 最高分    |
+-----------+-----------+
| 中文      |        95 |
| 英语      |        94 |
| 计算机    |        98 |
+-----------+-----------+
3 rows in set (0.00 sec)
#“max(grade)”关键字表示取“grade”的最高值
#“group by c_name”表示按科目进行聚合运算

8. 查询李四的考试科目(c_name)和考试成绩(grade)

mysql> select c.c_name 科目,c.grade 考试成绩 from score c,student s where c.stu_id=s.id and s.name='李四';
+-----------+--------------+
| 科目      | 考试成绩     |
+-----------+--------------+
| 计算机    |           70 |
| 英语      |           92 |
+-----------+--------------+
2 rows in set (0.00 sec)

9. 用连接的方式查询所有学生的信息和考试信息

mysql> select c.*,s.name,s.sex,s.birth,s.department,s.address from score c inner join student s on c.stu_id=s.id;
+----+--------+-----------+-------+-----------+------+-------+--------------+--------------------+
| id | stu_id | c_name    | grade | name      | sex  | birth | department   | address            |
+----+--------+-----------+-------+-----------+------+-------+--------------+--------------------+
|  1 |    901 | 计算机    |    98 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
|  2 |    901 | 英语      |    80 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
|  3 |    902 | 计算机    |    65 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
|  4 |    902 | 中文      |    88 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
|  5 |    903 | 中文      |    95 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
|  6 |    904 | 计算机    |    70 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
|  7 |    904 | 英语      |    92 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
|  8 |    905 | 英语      |    94 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
|  9 |    906 | 计算机    |    90 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
| 10 |    906 | 英语      |    85 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+----+--------+-----------+-------+-----------+------+-------+--------------+--------------------+
10 rows in set (0.00 sec)
#由于"c.stu_id"与"s.id"为重复值,所以不显示"s.id",保留“c.stu_id”
#由于“score”表存在同一学生对应不同科目,因此两表连接会存在重复值

10. 计算每个学生的总成绩

mysql> select s.name 姓名,sum(grade) 总成绩 from score c inner join student s on c.stu_id=s.id group by s.name;
+-----------+-----------+
| 姓名      | 总成绩    |
+-----------+-----------+
| 张三      |        95 |
| 张老二    |       153 |
| 张老大    |       178 |
| 李四      |       162 |
| 王五      |        94 |
| 王六      |       175 |
+-----------+-----------+
6 rows in set (0.00 sec)
#“sum(grade)”关键字表示求“grade”的和
#“group by s.name”表示按姓名进行聚合运算

11. 计算每个考试科目的平均成绩

mysql> select c.c_name 考试科目,avg(grade) 平均成绩 from score c inner join student s on c.stu_id=s.id group by c.c_name;
+--------------+--------------+
| 考试科目     | 平均成绩     |
+--------------+--------------+
| 中文         |      91.5000 |
| 英语         |      87.7500 |
| 计算机       |      80.7500 |
+--------------+--------------+
3 rows in set (0.00 sec)
#“sum(grade)”关键字表示求“grade”的平均值
#“group by c.c_name”表示按考试科目进行聚合运算

12. 查询计算机成绩低于95的学生信息

mysql> select s.* from score c inner join student s on c.stu_id=s.id where c.c_naame='计算机' and c.grade<95;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)

13. 查询同时参加计算机和英语考试的学生的信息

#方法一(推荐使用,效率高且运行相对查询较快):
mysql> select s.* from student s,(select * from score where c_name = '计算机') as st1 inner JOIN (select * from score where c_name = '英语') as st2 on st1.stu_id = st2.stu_id where s.id=st1.stu_id;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)
#将“score”作为多表查询的两者(自己连自己),从而进行两次筛选,最终筛出结果
#最后通过对学号的匹配查询出同时参加计算机和英语考试的学生的信息


#方法二(不推荐,步骤繁琐,效率低且相对查询较慢):
/*先利用“group_concat”将计算机和英语统计为一行显示*/
mysql> select stu_id,group_concat(c_name) 学科 from score group by stu_id;
+--------+------------------+
| stu_id | 学科             |
+--------+------------------+
|    901 | 计算机,英语      |
|    902 | 计算机,中文      |
|    903 | 中文             |
|    904 | 计算机,英语      |
|    905 | 英语             |
|    906 | 计算机,英语      |
+--------+------------------+
6 rows in set (0.00 sec)

/*再利用“having”筛选出同时参加计算机和英语考试的学生id*/
mysql> select stu_id,group_concat(c_name) 学科 from score group by stu_id having学科='计算机,英语';
+--------+------------------+
| stu_id | 学科             |
+--------+------------------+
|    901 | 计算机,英语      |
|    904 | 计算机,英语      |
|    906 | 计算机,英语      |
+--------+------------------+
3 rows in set (0.00 sec)

/*最后利用多表查询查询出同时参加计算机和英语考试的学生信息*/
mysql> select s.* from student s,(select stu_id,group_concat(c_name) 学科 from score group by stu_id having 学科='计算机,英语')x where x.stu_id=s.id;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)
#“(select stu_id,group_concat(c_name) 学科 from score group by stu_id having 学科='计算机,英语')x”表示将之前筛选出的结果作为一张新的表,再给这张新的表设置别名“x”

14. 将计算机考试成绩按从高到低进行排序

mysql> select grade 考试成绩 from score where c_name='计算机' order by grade desc;
;
+--------------+
| 考试成绩     |
+--------------+
|           98 |
|           90 |
|           70 |
|           65 |
+--------------+
4 rows in set (0.00 sec)
#查询语句末尾加了“order by grade asc”表示以科目成绩进行降序显示

15. 从student表和score表中查询出学生的学号,然后合并查询结果

mysql> select * from score c INNER JOIN student s ON c.stu_id=s.id;
+----+--------+-----------+-------+-----+-----------+------+-------+--------------+--------------------+
| id | stu_id | c_name    | grade | id  | name      | sex  | birth | department   | address            |
+----+--------+-----------+-------+-----+-----------+------+-------+--------------+--------------------+
|  1 |    901 | 计算机    |    98 | 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
|  2 |    901 | 英语      |    80 | 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
|  3 |    902 | 计算机    |    65 | 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
|  4 |    902 | 中文      |    88 | 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
|  5 |    903 | 中文      |    95 | 903 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
|  6 |    904 | 计算机    |    70 | 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
|  7 |    904 | 英语      |    92 | 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
|  8 |    905 | 英语      |    94 | 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
|  9 |    906 | 计算机    |    90 | 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
| 10 |    906 | 英语      |    85 | 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+----+--------+-----------+-------+-----+-----------+------+-------+--------------+--------------------+
10 rows in set (0.00 sec)
#普通的多表查询(连接),不做过多解析

16. 查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

mysql> select s.name 姓名,s.department 院系,c.c_name 考试科目,c.grade 科目成绩 from score c INNER JOIN student s ON c.stu_id=s.id where name like '张%' OR name like '王%'
';
+-----------+--------------+--------------+--------------+
| 姓名      | 院系         | 考试科目     | 科目成绩     |
+-----------+--------------+--------------+--------------+
| 张老大    | 计算机系     | 计算机       |           98 |
| 张老大    | 计算机系     | 英语         |           80 |
| 张老二    | 中文系       | 计算机       |           65 |
| 张老二    | 中文系       | 中文         |           88 |
| 张三      | 中文系       | 中文         |           95 |
| 王五      | 英语系       | 英语         |           94 |
| 王六      | 计算机系     | 计算机       |           90 |
| 王六      | 计算机系     | 英语         |           85 |
+-----------+--------------+--------------+--------------+
8 rows in set (0.00 sec)
#普通的多表查询(连接),不做过多解析

17. 查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

mysql> select s.name 姓名,(year(now())-s.birth) 年龄,s.department 院系,c.c_name 考试科目,c.grade 科目成绩 from score c INNER JOIN student s ON c.stu_id=s.id where s.addresss 
+--------+--------+--------------+--------------+--------------+
| 姓名   | 年龄   | 院系         | 考试科目     | 科目成绩     |
+--------+--------+--------------+--------------+--------------+
| 张三   |     32 | 中文系       | 中文         |           95 |
| 王六   |     34 | 计算机系     | 计算机       |           90 |
| 王六   |     34 | 计算机系     | 英语         |           85 |
+--------+--------+--------------+--------------+--------------+
3 rows in set (0.00 sec)
#“year()”函数表示输出年份,括号内参数需要指定,“now()”函数表示输出当前时间,两者结合表示输出当前年份
#使用(year(now())-s.birth)可求出每个学生对应的年龄,然后设置别名为年龄
举报

相关推荐

0 条评论