+-------------------+
| Tables_in_dapeng3 |
+-------------------+
| class |
| course |
| s1 |
| score |
| student |
| teacher |
+-------------------+
6 rows in set (0.00 sec)

mysql> select * from class;
+----+--------------+
| id | caption |
+----+--------------+
| 1 | 三年二班 |
| 2 | 三年三班 |
| 3 | 一年二班 |
+----+--------------+
3 rows in set (0.11 sec)

mysql> select * from course;
+----+--------+------------+
| id | cname | teacher_id |
+----+--------+------------+
| 1 | 生物 | 1 |
| 2 | 物理 | 2 |
| 3 | 体育 | 3 |
| 4 | 美术 | 2 |
+----+--------+------------+
4 rows in set (0.00 sec)

mysql> select * from student;
+----+--------+--------+----------+
| id | sname | gender | class_id |
+----+--------+--------+----------+
| 1 | 铁蛋 | male | 2 |
| 2 | 钢蛋 | female | 1 |
| 3 | 铜蛋 | male | 3 |
| 4 | 银弹 | male | 1 |
| 5 | ety | male | 1 |
| 6 | yuu | female | 2 |
+----+--------+--------+----------+
6 rows in set (0.11 sec)

mysql> select * from teacher;
+----+-----------------+
| id | tname |
+----+-----------------+
| 1 | 张磊老师 |
| 2 | 李平老师 |
| 3 | 刘海燕老师 |
| 4 | 朱云海老师 |
| 5 | 李杰老师 |
+----+-----------------+
5 rows in set (0.11 sec)

mysql> select * from score;
+----+------------+-----------+--------+
| id | student_id | course_id | number |
+----+------------+-----------+--------+
| 1 | 1 | 1 | 100 |
| 2 | 2 | 1 | 58 |
| 3 | 2 | 2 | 56 |
| 4 | 3 | 3 | 90 |
+----+------------+-----------+--------+


1、查询所有的课程的名称以及对应的任课老师姓名
mysql> select course.cname,teacher.tname from course inner join teacher on course.teacher_id = teacher.id;
+--------+-----------------+
| cname | tname |
+--------+-----------------+
| 生物 | 张磊老师 |
| 物理 | 李平老师 |
| 美术 | 李平老师 |
| 体育 | 刘海燕老师 |
+--------+-----------------+ 2、查询学生表中男女生各有多少人
mysql> select gender,count(gender) as male_count from student where gender = 'male' group by gender ;
+--------+------------+
| gender | male_count |
+--------+------------+
| male | 4 |
+--------+------------+
1 row in set (0.00 sec) mysql> select gender,count(gender) as male_count from student where gender = 'female' group by gender ;
+--------+------------+
| gender | male_count |
+--------+------------+
| female | 2 |
+--------+------------+ 3、查询物理成绩等于100的学生的姓名
mysql> select sname from (select score.student_id,score.number,student.id,student.sname
from score inner join student where student.id = score.student_id) as t1 where number = 100;
+--------+
| sname |
+--------+
| 铁蛋 |
+--------+ 4、查询成绩大于八十分的同学的姓名和平均成绩
mysql> select student.id,student.sname,score.student_id,score.number from student inner join
score on student.id = score.student_id where score.number > 80 ;
+----+--------+------------+--------+
| id | sname | student_id | number |
+----+--------+------------+--------+
| 3 | 铜蛋 | 3 | 90 |
| 1 | 铁蛋 | 1 | 100 |
| 4 | 银弹 | 4 | 98 |
+----+--------+------------+--------+ mysql> select avg(number) from (select student.id,student.sname,score.student_id,score.number
from student inner join score on student.id = score.student_id where score.number > 80) as t;
+-------------+
| avg(number) |
+-------------+
| 96.0000 |
+-------------+ 5、查询所有学生的学号,姓名,总成绩
mysql> select student.id,student.sname,t1.total_num from student left join(select score.student_id,
sum(score.number) as total_num from score group by score.student_id) as t1 on student.id = t1.student_id;
+----+--------+-----------+
| id | sname | total_num |
+----+--------+-----------+
| 1 | 铁蛋 | 100 |
| 2 | 钢蛋 | 80 |
| 3 | 铜蛋 | 90 |
| 4 | 银弹 | 98 |
| 5 | ety | NULL |
| 6 | yuu | NULL |
+----+--------+-----------+ 6、 查询姓李老师的个数
mysql> select tname from teacher where tname like '李%';
+--------------+
| tname |
+--------------+
| 李平老师 |
| 李杰老师 |
+--------------+ 7、 查询没有报李平老师课的学生姓名(未完成)
结果:学生姓名
查看李平老师教授的课程,然后和学生表建立关联
select student.sname,student.id from student left join
(select teacher.id,course.teacher_id,course.cname,teacher.tname from teacher
left join course on teacher.id = course.teacher_id) as t1 ; 8、 查询物理课程比生物课程高的学生的学号
结果:学生的学号-student.id
score绑定课程
mysql> select t1.student_id from (select student_id,number from score
where course_id = (select id from course where cname = '物理')) as t1 inner join
(select student_id,number from score where course_id = (select id from course where cname = '生物'))
as t2 on t1.student_id = t2.student_id where t1.number > t2.number;
+------------+
| student_id |
+------------+
| 2 |
+------------+
9、 查询没有同时选修物理课程和体育课程的学生姓名
结果:学生姓名
select student.sname from student where id in(select student_id from score where
course_id in(select id from course where cname = '物理' or cname = '体育')
group by student_id having count(course_id) = 1); 10、查询挂科超过两门(包括两门)的学生姓名和班级
结果:学生姓名和班级
mysql> select student.sname,class.caption from student inner join(select student_id from score where number <60
group by student_id having count(course_id) >= 2) as t1 inner join class on student.id = t1.student_id and
student.class_id = class.id;
流程注释:select 字段 from table. table里有学生姓名和班级,假设先把学生表和班级表连接起来,他们有共同字段class_id,内连接后可得到一张
学生对应班级的表。然后查询score表里的学生的每门成绩,如果有两门不到60的,获得该学生student_id,然后把上个表当作参数传入。
+--------+--------------+
| sname | caption |
+--------+--------------+
| 钢蛋 | 三年二班 |
+--------+--------------+ 11 、查询选修了所有课程的学生姓名
mysql> select student.sname from student where id in (select student_id from score group by student_id having
count(course_id) = (select count(id) from course));
Empty set (0.14 sec) 12、查询李平老师教的课程的所有成绩记录
mysql> select * from score where course_id in (select id from course inner join
teacher on course.teacher_id = teacher.id where teacher.tname = '李平老师'); 13、查询全部学生都选修了的课程号和课程名
mysql> select id,cname from course where id in (select course_id from score group by course_id
having count(student_id) = (select count(id) from student));
Empty set (0.01 sec) 14、查询每门课程被选修的次数
mysql> select course_id,count(student_id) from score group by course_id;
+-----------+-------------------+
| course_id | count(student_id) |
+-----------+-------------------+
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
+-----------+-------------------+ 15、查询只选修了一门课程的学生姓名和学号
mysql> select id,sname from student where id in (select student_id from score group by student_id having count(course_id) = 1);
+----+--------+
| id | sname |
+----+--------+
| 1 | 铁蛋 |
| 3 | 铜蛋 |
+----+--------+ 16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
mysql> select distinct number from score order by number desc;
+--------+
| number |
+--------+
| 100 |
| 90 |
| 58 |
| 56 |
+--------+ 17、查询平均成绩大于85的学生姓名和平均成绩
mysql> select sname,t1.avg_num from student inner join (select student_id,avg(number) as avg_num from score group by student_id having avg(number) >85) as t1 on student.id = t1.student_id;
+--------+----------+
| sname | avg_num |
+--------+----------+
| 铁蛋 | 100.0000 |
| 铜蛋 | 90.0000 |
+--------+----------+
2 rows in set (0.11 sec) 18、查询生物成绩不及格的学生姓名和对应生物分数
SELECT
sname ,
number
FROM
score
LEFT JOIN course ON score.course_id = course.id
LEFT JOIN student ON score.student_id = student.id
WHERE
course.cname = '生物'
AND score.number < 60;
+--------+--------+
| sname | number |
+--------+--------+
| 钢蛋 | 58 |
+--------+--------+ 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
SELECT
sname
FROM
student
WHERE
id = (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
course.id
FROM
course
INNER JOIN teacher ON course.teacher_id = teacher.id
WHERE
teacher.tname = '李平老师'
)
GROUP BY
student_id
ORDER BY
AVG(number) DESC
LIMIT 1
); +--------+
| sname |
+--------+
| 钢蛋 |
+--------+ 20、查询每门课程成绩最好的前两名学生姓名

mysql多表查询20题的更多相关文章

  1. python 3 mysql 单表查询

    python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...

  2. python3 mysql 多表查询

    python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...

  3. MySQL多表查询之外键、表连接、子查询、索引

    MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...

  4. Mysql 单表查询 子查询 关联查询

    数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...

  5. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  6. Mysql 单表查询where初识

    Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...

  7. MySQL多表查询合并结果union all,内连接查询

    MySQL多表查询合并结果和内连接查询 1.使用union和union all合并两个查询结果:select 字段名 from tablename1 union select 字段名 from tab ...

  8. MySQL多表查询、事务、DCL:内含mysql如果忘记密码解决方案

    MySQL多表查询.事务.DCL 多表查询 * 查询语法: select 列名列表 from 表名列表 where.... * 准备sql # 创建部门表 CREATE TABLE dept( id ...

  9. (转)Mysql 多表查询详解

    MySQL 多表查询详解 一.前言  二.示例 三.注意事项 一.前言  上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...

随机推荐

  1. redis10---Setbit 的实际应用

    Setbit 的实际应用 场景: 1亿个用户, 每个用户 登陆/做任意操作 ,记为 今天活跃,否则记为不活跃 每周评出: 有奖活跃用户: 连续7天活动,每月评,等等. 思路: Userid dt ac ...

  2. adb client, adb server, adbd原理浅析(附带我的操作过程)【转】

    本文转载自:http://blog.csdn.net/stpeace/article/details/24933813 adb是什么? adb就是Android调试桥,很形象啊. 先来看adb原理的逻 ...

  3. 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  4. HUST - 1010 The Minimum Length(最小循环节)

    1.赤裸裸的最小循环节 2. 3. #include<iostream> #include<stdio.h> #include<string.h> using na ...

  5. 前端CSS规范整理

    一.文件规范 1.文件均归档至约定的目录中. 具体要求通过豆瓣的CSS规范进行讲解: 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用U ...

  6. 「LuoguP4753」濑 River Jumping(贪心

    Description 有一条宽度为 N 的河上,小D位于坐标为 0 的河岸上,他想到达坐标为 N 的河岸上后再回到坐标为 0 的位置.在到达坐标为 N 的河岸之前小D只能向坐标更大的位置跳跃,在到达 ...

  7. 微信小程序在线支付功能使用总结

    最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...

  8. appium九宫格解锁错误提示:The coordinates provided to an interactions operation are invalid解决办法

    原文地址:http://blog.csdn.net/qqtMJK/article/details/77838814 今天做自动化解锁9宫格,发现swipe不能满足需求,于是用TouchAction去实 ...

  9. Linux系统安装完的调整和安全

    精简开机系统自启动 •五个企业环境中开机自启动的服务; sshd:远程连接linux服务器必须开启 rsyslog:日志相关软件 network:网络服务 crond:系统和用户配置的计划任务周期性进 ...

  10. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时26&&27

    课时26 图像分割与注意力模型(上) 语义分割:我们有输入图像和固定的几个图像分类,任务是我们想要输入一个图像,然后我们要标记每个像素所属的标签为固定数据类中的一个 使用卷积神经,网络为每个小区块进行 ...