①①查询每门课的平均成绩
Select avg(degree) from score where cno='3-105';      # 仅能查询一门课的成绩
Select cno,avg(degree) from score group by cno;    # group by 为分组
①②查询score表中至少有2名学生选修的并以3开头的课程的平均分数
Select cno,avg(degree),count(*) from score group by cno    # count(*)显示匹配到的cno个数
Having count(cno)>=2 and cno like '3%';    # 现有两个cno数据,并且用正则表达式表示以3开头,like为模糊查找
①③查询分数大于70,小于90的sno列
Select sno,degree from score
Where degree>70 and degree<90;
--或 where degree between 70 and 90;
①④查询所有学生的sname、cno和degree列
Select sno,sname from student;
Select sno,cno,degree from score;

多表查询:

Select sname,cno,degree from student,score
Where student.sno=score.sno;    # 将两张表的sno对等去重
①⑤查询所有的sno、cname和degree列
Select sno,cname,degree from course,score
Where course.cno = score.cno;
①⑥查询所有学生的sname、cname和degree列
select sname,cname,degree from student,course,score
Where student.sno=score.sno and course.cno=score.cno;     # 将这三张表中的学生连到一起,其中的函数必须已经用外键连接到一起。
①⑦查询95031班学生每门课的平均分
Select cno,avg(degree) from score
where sno in (select sno from student where class='95031')     # 先查询课程号为95031的学生
Group by cno;
①⑧查询选修’3-105’课程的成绩高于’109’号同学’3-105’成绩的所有同学的记录。
Select degree from score where sno='109' and cno='3-105';Select * from score where cno='3-105' and degree>(Select degree from score where sno='109' and cno='3-105');
①⑨查询成绩高于学号为’109’、课程号为’3-105’的成绩的所有记录
Select * from score where degree >(Select degree from score where sno='109' and cno='3-105');
②⑩查询与学号108、101的同学同年出生的所有学生的sno、sname和sbirthday列。
Select sno,sname,year(sbirthday) from student where sno in ('108','101');     # 查找年份
Select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in ('108','101'));  查找年份对应的人
②①查询‘张旭’教师任课的学生成绩
Select tno from teacher where tname='张旭';     # 查询教师编号
Select cno from course where tno=(Select tno from teacher where tname='张旭');      # 查询课程编号
Select * from score where cno=(Select cno from course where tno=(Select tno from teacher where tname='张旭'));      # 查询成绩
②②查询选修某课程的同学人数多于5人的教师姓名
Select * from teacher where tno=(Select tno from course where cno=(Select cno from score group by cno having count(*)>2));
②③查询95033班和95031班全体学生的记录
Select * from student where class in ('95031','95038');
②④查询存在有85分以上成绩的课程cno
Select distinct cno from score where degree>85;
②⑤查询出“计算机系”教师所教课程的成绩表
Select * from score where Cno in (Select cno from course where Tno in (Select tno from teacher where depart='计算机系'));
②⑥查询“计算机系”与"电子工程系"不同职称的教师的tname prof
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='计算机系');
②⑦查询选修编号为’3-105’课程且成绩至少高于选修编号为’3-245’任意一个的同学的cno、sno和degree,并按Degree从高到低次序排序。
Select * from score where cno='3-245';
Select * from score where cno='3-105' and degree>any(Select degree from score where cno='3-245') order by degree desc;
②⑧查询选修编号为’3-105’且成绩高于选修编号为’3-245’课程的同学的cno、sno和degree
Select * from score
Where cno='3-105'
And degree>all(select degree from score where cno='3-245');
②⑨查询所有教师和同学的name、sex、birthday–别名
Select tname as name,tsex as sex,tbirthday as birthday from teacher
union
Select sname,ssex,sbirthday from student;
③⑩查询所有’女’教师和’女’同学的name、sex、birthday
Select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女'
union
Select sname,ssex,sbirthday from student where ssex='女';

MYSQL 查询练习②相关推荐

  1. mysql查询字段大小写结果相同,mysql大小写查询不敏感,mysql5.7查询不区分大小写解决方案。

    下面有两条sql,主键查询,在mysql中查询到的结果相同. SELECT* FROM USER WHEREid = 'EM58hdK4nXC';SELECT* FROM USER WHEREid = ...

  2. MySQL查询进阶之多表查询

    一.多表查询 1.引出 2.笛卡尔积 3. 笛卡尔积的解决方法 二.多表查询分类 1.等值连接和非等值连接 2.自连接和非自连接 3.内连接和外连接 SQL92:使用(+)创建连接 SQL99语法实现 ...

  3. smarty mysql_Smarty处理mysql查询数组

    Smarty处理mysql查询数组 MySQL的查询结果一般是一个数组,而不是所有结果集.因此我们需要将结果全部存到数组中进行处理,然后就可以很轻松的再Smarty中使用了. PHP Mysql 代码 ...

  4. MYSQL 查询数据排序数据和分组数据

    在mysql查询过程中,可以对数据进行过滤,也可以对数据进行排序,可以对数据分组,下面分别讲述排序数据和分组数据例子.1,数据的排序 使用 ORDER BYselect * from where id ...

  5. mysql查询解析过程_MySQL查询执行过程详解

    查询是用户通过设置某些查询条件,从表或其他查询中选取全部或者部分数据,以表的形式显示数据供用户浏览.查询是一个独立的.功能强大的.具有计算功能和条件检索功能的数据库对象.MySQL数据库中,MySQL ...

  6. mysql 查询某个日期时间段,每天同一时间段的数据

    mysql 查询某个日期时间段,每天同一时间段的数据: SELECT * FROM t_a01_eltable WHERE DATE_FORMAT(acqtime,'%Y-%m-%d')>='2 ...

  7. php 多条查询结果插入新表,Mysql应用MySQL查询结果复制到新表的方法(更新、插入)...

    <Mysql应用MySQL查询结果复制到新表的方法(更新.插入)>要点: 本文介绍了Mysql应用MySQL查询结果复制到新表的方法(更新.插入),希望对您有用.如果有疑问,可以联系我们. ...

  8. MySQL查询区分大小写

    2019独角兽企业重金招聘Python工程师标准>>> 问题描述: 找出用户名id为'AAMkADExM2M5NjQ2LWUzYzctNDFkMC1h'的用户的数据: select ...

  9. 100G内存下,MySQL查询200G大表会OOM么?

    文章来源:https://sourl.cn/vwDNzn 我的主机内存只有100G,现在要全表扫描一个200G大表,会不会把DB主机的内存用光? 逻辑备份时,可不就是做整库扫描吗?若这样就会把内存吃光 ...

  10. mysql 查询用户最后登陆时间_弄懂mysql:mysql的通信协议

    我准备从mysql的实现出发,将mysql好好理解一下,从他的逻辑结构一层一层出发,感受一下,所以再学第一层之前,要先对mysql整体的逻辑结构有一个初步认识 mysql逻辑架构 整体来说,MySql ...

最新文章

  1. 打开数“智”化之门,一字之差带来的思考
  2. python入门基础代码图-python入门基础知识(代码)
  3. mysql的status状态说明
  4. Java设计模式(二) -- 单例模式
  5. 关于J2EE中死锁问题的研究(2)
  6. java传.net datetime_.net调java写的webService传过去的datetime,int等非string类型为null的问题...
  7. elfutils cc1: all warnings being treated as errors
  8. python django 优势_为什么选择Django?
  9. 《如何建立自己的算法交易事业》读书笔记
  10. 多线多IP的服务器配置
  11. swift中的let和var有什么区别?
  12. 中国计量大学研究生复试c语言_中国计量大学2020考研招调剂生,25个专业
  13. mysql 出现ERROR 2002 (HY000): ....错误通用解决方法
  14. 西南科技大学OJ题 插入排序算法实现1016
  15. 新能源汽车动力电池(热管理)热流体仿真分析-基于SCDM和STAR-CCM+热仿真分析课程(评论发链接)
  16. python rtf转txt_批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹...
  17. 咸鱼Maya笔记—灯光效果
  18. 手把手教你在Linux系统进行项目部署
  19. 《郭论—捡史》郭德纲/著 读后得
  20. 小程序“扫码购”的自助收银模式可以为商家带来什么?

热门文章

  1. 驴妈妈客户端频道页模块化设计思路
  2. 第十一章数据仓库和商务智能
  3. 3131: [Sdoi2013]淘金
  4. 运营总监直言:再招聘到不懂数据分析的运营,直接开除
  5. 解决java的非法字符: ‘\u200b‘的问题
  6. Ubuntu20.04下安装QQ
  7. php文件防止修改,php文件上传限制修改
  8. 【思维进阶】这些年给学员的毕业赠言(一)
  9. N级台阶,一次上1级或2级或3级或M级,总共有多少种走法
  10. QQ空间照片上传HTML代码演示