mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html
时间:2020-04-06
本文章向大家介绍mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html,主要包括mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
# 1、查询男生、女生的人数;
# select gender,count(*) from student group by gender;
# 2、查询姓“张”的学生名单;
# select * from student where sname like '张%';
# 3、课程平均分从高到低显示
# select course_id,avg(num) as a from score group by course_id order by a desc; #查出了各个部门的平均分
# 4、查询有课程成绩小于60分的同学的学号、姓名;
# select sid,sname from student where sid in (select distinct student_id from score where num<60);
# 5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;
# 学号1学的课程 select course_id from score where student_id =1;
# 相同课程的学号 select distinct student_id from score where course_id in (select course_id from score where student_id =1);
# select sid,sname from student where sid in(select distinct student_id from score where course_id in (select course_id from score where student_id =1) and student_id !=1);
# 6、查询出只选修了一门课程的全部学生的学号和姓名;
# #学生们的选课情况 select student_id from score group by student_id having count(sid)=1;
# # having select sname,sid from student where sid =(select student_id from score group by student_id having count(sid)=1);
# 7、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
# select course_id,max(num),min(num) from score group by course_id;
# 8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
# 把课程1,2的所有人的学生id,成绩找出来
# select student_id as sid1,num as n1 from score where course_id =1 ;
# select student_id as sid2,num as n2 from score where course_id =2 ;
# 取课程2成绩比课程1低的所有同学的学好
# select sid1 from( select student_id as sid1,num as n1 from score where course_id =1)as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id =2) as t2
# on t1.sid1=t2.sid2 where n2<n1;
# 连表student表取名字和学号
# select sid,sname from student right join(
# select sid1 from( select student_id as sid1,num as n1 from score where course_id =1)as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id =2) as t2
# on t1.sid1=t2.sid2 where n2<n1
# )as tmp
# on student.sid = tmp.sid1;
# 9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
# 现获取生生物课,物理课的id
# select cid from course where cname='生物';
# select cid from course where cname='物理';
# 找到所有血生物,物理的人的学号和分数
# select student_id as sid1,num as n1 from score where course_id=(select cid from course where cname='生物')
# select student_id as sid2,num as n2 from score where course_id=(select cid from course where cname='物理')
# select sid from student right join(
# select sid1 from (select student_id as sid1,num as n1 from score where course_id=(select cid from course where cname='生物')) as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id=(select cid from course where cname='物理'))as t2
# on t1.sid1=t2.sid2 where n1 >n2
# )as tmp
# on student.sid=tmp.sid1
# 10、查询平均成绩大于60分的同学的学号和平均成绩;
# select sid,avg(num) from score group by student_id having avg(num)>60;
# 11、查询所有同学的学号、姓名、选课数、总成绩;
# select student_id,count(course_id)as '选课数',sum(num)as '总成绩' from score group by student_id
# select student_id,sname,c,b from student as t1
# inner join
# (select student_id,count(course_id)as c,sum(num)as b from score group by student_id)as t2
# on t1.sid=t2.student_id
# 12、查询姓“李”的老师的个数;
# select count(tid) from teacher where tname like '李%';
# 13、查询没学过“张磊老师”课的同学的学号、姓名;
# 张磊老师教的课程 select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师');
# 学过张磊老师课的同学的student_id select student_id from score where course_id=( select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师'));
# 没学过的同学id select sid,sname from student where sid not in (select student_id from score where course_id=( select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师')));
# 14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
# 学过1的同学 select student_id from score where course_id =1;
# 是否学过2的同学学号 select student_id from score where student_id in (select student_id from score where course_id =1) and course_id=2;
# 姓名 select sid,sname from student where sid in(select student_id from score where student_id in (select student_id from score where course_id =1) and course_id=2);
# 15、查询学过“李平老师”所教的所有课的同学的学号、姓名;
# 李平老师教的课 select cid from course where teacher_id=(select tid from teacher where tname='李平老师');
# 学过李平老师课的学生 select distinct student_id from score where course_id in (select cid from course where teacher_id=(select tid from teacher where tname='李平老师'));
# 学生的学号和姓名 select sid,sname from student where sid in(select distinct student_id from score where course_id in (select cid from course where teacher_id=(select tid from teacher where tname='李平老师')));
原文地址:https://www.cnblogs.com/zhaolei1986/p/12640660.html
- HTML 教程
- HTML 简介
- html div 标签介绍
- html span 标签介绍
- html a 超链接标签
- HTML Br换行标签介绍
- HTML P段落标签介绍
- HTML br与p标签区别
- Html H 标题标签
- html px em pt长度单位
- HTML form 标签
- HTML radio 单选框
- HTML B 加粗标签
- HTML strong加粗粗体标签
- HTML em 强调标签
- HTML i 斜体标签
- HTML u下划线标签
- HTML s 删除线标签
- Html img 图片标签
- Html上标注sup与下标注sub标签
- HTML nobr 禁止换行标签
- HTML hr 水平线标签
- HTML label 标签
- HTML input 标签
- HTML textarea 标签
- HTML select下拉列表标签
- HTML checkbox 多选框
- HTML font color 标签
- HTML iframe 框架标签
- HTML Table 表格
- HTML dl dt dd 标签
- HTML ol li有序列表标签
- HTML ul li 无序列表标签
- HTML 注释
- CSS 教程
- CSS 简介
- CSS 语法
- CSS Id 和 Class选择器
- CSS 样式的创建
- CSS background 背景介绍
- CSS 文本样式
- CSS font 字体
- CSS A 链接
- CSS ul ol列表样式
- CSS TABLE 样式
- CSS 框模型
- CSS border 边框
- CSS Outlines 轮廓
- CSS 外边距 Margin
- CSS Padding 内边距
- CSS 分组和嵌套选择器
- CSS 尺寸 (Dimension)
- CSS Display 属性
- CSS Position 定位
- CSS Float 浮动
- CSS 水平对齐(Horizontal Align)
- CSS 组合选择符
- CSS 伪类
- CSS 伪元素
- CSS 导航栏
- CSS 下拉菜单
- CSS 图片廊
- CSS 图像透明/不透明
- CSS sprite 图像拼合技术
- CSS 媒体类型
- CSS 属性选择器
- CSS 实例
- python 微信机器人-如何调用机器人的api,调用图灵机器人接口演示。调用机器人原理,图灵机器人注册。
- Python 技术篇-如何查看python库包含什么方法,python库有哪些用法,python库的属性。
- Android Studio 安装配置教程 - MacOS(详细版)
- Android 天气APP(十六)热门城市 - 海外城市
- Android 天气APP(十七)热门城市 - 国内城市
- Python 技术篇-PIL库安装及截图功能演示
- Android 天气APP(十八)常用城市
- Android 蓝牙开发(扫描设备、绑定、解绑)
- Python 微信机器人:调用电脑摄像头时时监控功能实现演示,调用电脑摄像头进行拍照并保存。
- Android 天气APP(十九)更换新版API接口(更高、更快、更强)
- Android 天气APP(二十)增加欢迎页及白屏黑屏处理、展示世界国家/地区的城市数据
- Android 天气APP(二十二)改动些许UI、增加更多空气质量数据和生活建议数据展示
- Android 自定义View 之 RectF用法详解
- Android 天气APP(二十五)地图天气(下)嵌套滑动布局渲染天气数据
- Android 天气APP(二十六)增加自动更新(检查版本、通知栏下载、自动安装)