mysql带条件查询,联表查询

---恢复内容开始---

1,用于设定所select出来的数据是否允许出现重复行(完全相同的数据行)

all:允许出现——默认不写就是All(允许的)。

distinct:不允许出现——就是所谓的“消除重复行”

2,where:条件

3,group by:分组依据 后面加表的字段名,通常只进行一个字段的分组

mysql表查询语法形式:select [all | distinct] 字段名或表达式 from 表名 [where] [group by] [having] [order by] [limit];

练习题:共有下面四张表    学生表:student  教师表:teacher  课程表:course 成绩表:score

          

1,查询Score表中至少有5名学生选修的并以3开头的课程的平均分数

--操作表score,以cno分组并且cno是以3开头,取出各个cno的总数,及与cno对应的degree的平均值

select count(cno),avg(degree) from score where cno like '3%' group by cno;

 

--从上面的虚拟表中找到cno总数大于5的

select * from

(select count(cno) a,avg(degree) b from score where cno like '3%' group by cno) c

where a>5;

2,查询所有学生的Sno、Cname和Degree列

--找到student的sno
 select sno from student;


 --找到score 的sno,degree,cno
 select sno,degree,cno from score;


 --找到course的cno
 select cno,cname from course;


 --组成新表cno sno degree
 select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno;


 --组成有cname cno sn degree sname的表
 select d.cname,e.cno,e.sno,e.degree,e.sname
 from
   (select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno) as e
  join
  (select cname,cno from course) as d
  on d.cno=e.cno;

3,查询“95033”班学生的平均分

--从student取sno class,条件是class是95033的

select sno,class from student where class='95033';

--取出score中sno degree

select sno,degree from score;

--将上面两张表组成一张,取degree的平均值

select avg(degree) from

(select sno,class from student where class='95033') a

join

(select sno,degree from score) b

on a.sno=b.sno;

4,查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

--将109号的成绩取出

select degree from score where sno='109' and cno='3-105';

-- 得出最终表

select * from score

where

degree>(select degree from score where sno='109' and cno='3-105')

and cno='3-105';

5,查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列

--找到student中sno为108 的Sbirthday
select year(sbirthday) from student where sno='108';


-- 得出最终表
select sno,sname,sbirthday from student

where

year(sbirthday)=(select year(sbirthday) from student where sno='108');

 6.查询“张旭“教师任课的学生成绩(姓名)

--找到teacher中tname为张旭的tno

select tno from teacher where tname='张旭';

--找到course中tno和teacher中tname为张旭的tno相同的cno

select cno from course where tno=(select tno from teacher where tname='张旭');

--找到score中cno为6-166的sno cno degree

select sno,cno,degree from score

where

cno=(select cno from course where tno=(select tno from teacher where tname='张旭'));

--找到student表中与上表sno相同的sname与上表组成新表

select a.sname,b.sno,b.cno,b.degree from

(select sno,sname from student) a

join

(select sno sno,cno,degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='张旭'))) b

on a.sno=b.sno;

7,查询考计算机导论的学生成绩
--找到course中cname为计算机导论的cno
select cno from course where cname='计算机导论';


--找到score中cno与上表中相同时的sno degree
select sno,degree from score where cno=(select cno from course where cname='计算机导论');


-- 得出最终表
select b.sname,a.sno,a.degree from

(select sno,degree from score where

cno=(select cno from course where cname='计算机导论')) a join (select sname,sno from student) b

on a.sno=b.sno;


8,查询李诚老师教的课程名称
select tno from teacher where tname='李诚';
select cname,cno from course where tno=(select tno from teacher  where tname='李诚');


9,查询选修某课程的同学人数多于5人的教师姓名
--score以cno分组,统计cno的数量
select count(cno) a,cno from score group by cno;


--找到上表中cno数量大于5的的cno
select cno from (select count(cno) a,cno from score group by cno) b where a>5;


--找到与上表cno一致的表course表中的tno
select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5);


--与上表tno一致的表teacher中的tname
select tname from teacher where tno=(select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5));


10,查询95033班和95031班全体学生的记录
--找到表score中的sno和cno 和degree
select sno,cno,degree from score;
--将上表和student组合
select a.sno,a.sname,a.ssex,a.sbirthday,a.class,b.cno,b.degree from

(select * from student) a join (select cno,degree,sno from score) b

on a.sno=b.sno;


11,查询存在有85分以上成绩的课程Cno
select cno,degree from score where degree>85;


12,查询出“计算机系“教师所教课程的成绩表
--找到teacher中的tno tname
select tno,tname from teacher where dapart='计算机系';
--找到course中tno和上表相同的cno
select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='计算机系') b on a.tno=b.tno;
--找到score中cno与上表相同的degree
select c.sno,c.cno,c.degree,d.tname from (select sno,cno,degree from score) c join (select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='计算机系') b on a.tno=b.tno) d on c.cno=d.cno;


13,查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学    的Cno、Sno和Degree,并按Degree从高到低次序排序
--score中找到cno=3-245的degree,取出最小值
select min(degree) from score where cno='3-245';
--score中找到cno=3-105的cno sno degree,并且degree大于上表的degree
select cno,sno,degree from

score where cno='3-105'

and degree>(select min(degree) from score where cno='3-245')

order by degree desc;


14,查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的    Cno、Sno和Degree.
--找到score中cno=3-245中的degree的最大值
select max(degree) from score where cno='3-245';
--找到score中cno=3-105的cno sno degree
select cno,sno,degree from

score where

degree>(select max(degree) from score where cno='3-245') order by degree asc;


15,查询所有教师和同学的name、sex和birthday
select tname,tsex,tbirthday from teacher
union
select sname,ssex,sbirthday from student;
16,查询所有“女”教师和“女”同学的name、sex和birthday
select tname,tsex,tbirthday from teacher where tsex='女'
union
select sname,ssex,sbirthday from student where ssex='女';


17,查询成绩比该课程平均成绩低的同学的成绩表
select avg(degree) from score where cno='3-105';
select avg(degree) from score where cno='3-245';
select avg(degree) from score where cno='6-166';
select * from score where cno='3-105' and degree<(select avg(degree) from score where cno='3-105')
union
select * from score where cno='3-245' and degree<(select avg(degree) from score where cno='3-245')
union
select * from score where cno='6-166' and degree<(select avg(degree) from score where cno='6-166');


18,查询所有任课教师的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


19,查询所有未讲课的教师的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


20,查询至少有2名男生的班号
select count(ssex),class from student where ssex='男' group by class;


select b.class from (select count(ssex) a,class from student where ssex='男' group by class) b where a>=2;


21,查询Student表中不姓“王”的同学记录
select * from student where sname not like '王%';


22,查询Student表中每个学生的姓名和年龄
select  sname,year(now())-year(sbirthday) from student;


23,查询Student表中最大和最小的Sbirthday日期值
select min(day(sbirthday)),max(day(sbirthday)) from student;


24,以班号和年龄从大到小的顺序查询Student表中的全部记录
select * from student order by class desc,date(sbirthday) asc;


25,查询“男”教师及其所上的课程
select tno from teacher where tsex='男';
select a.tno,b.cname,a.tname from (select tno,tname from teacher where tsex='男') a join (select * from course) b on a.tno=b.tno;

 

转载于:https://www.cnblogs.com/wfc139/p/8939605.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250420.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

day11-元组与字典

1、元组Tuple与列表类似&#xff0c;不同之处在于元组的元素不能修改。 元组使用小括号&#xff0c;列表使用中括号。元组可以查询&#xff0c;可以使用内置函数count、index。但是不能修改、增加、删除&#xff08;儿子不能&#xff0c;孙子有可能&#xff09;。name (a,a,b)…

算法 --- [map的使用]求最大和谐子序列

说明 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。 现在&#xff0c;给定一个整数数组&#xff0c;你需要在所有可能的子序列中找到最长的和谐子序列的长度。 输入: [1,3,2,2,5,2,3,7] 输出: 5 原因: 最长的和谐数组是&#xff1a;[3,2,2,2,3]. 思路 创…

vue问题四:富文本编辑器上传图片

vue使用富文本编辑器上传图片&#xff1a; 我是用的是wangEditor 富文本编辑器 demo:http://www.wangeditor.com/ 1).安装依赖:npm install wangeditor 2).我自己是创建了一个组件这样再用到的时候可以直接调用&#xff08;可能有更简单的方法&#xff09; <template lang&q…

R 实用命令 1

Quit and restart a clean R session from within R? If youre in RStudio: command/ctrl shift F10 .rs.restartR()转载于:https://www.cnblogs.com/shuaihe/p/8945039.html

vscode --- 快捷键格式化代码时,分号消失

问题复现 最近在vscode中,格式化代码(快捷键 alt shift F)时,分号会莫名奇妙的消失 对于习惯打分号的我来说,看起来很别扭… 解决方案. 我使用的是prettier这个插件来设置格式化的.安装方法如下: 点击左侧的: 搜索 prettier, 选择 Prettier - Code formatter 安装好了之后…

【python开发】构造一个可以查看,填加和返回的字典

当我们在面对一个字典的时候&#xff0c;基本功能有查找&#xff0c;填加&#xff0c;和返回上一级&#xff0c;我们利用上一篇的字典&#xff0c;写了一个可以实现字典基本功能的小程序&#xff1a; #!/usr/bin/env python # -*- coding:utf-8 -*- dp {亚洲:{中国:{山东:{},北…

算法 --- [队列结构]二叉树的层次遍历

思路 使用队列: 初始化的时候,将root, push进队列q中循环队列q,当其中不为空时,取出第一个元素(q.shift),记为r若r.left不为空,将r.left推进q,若r.right不为空,将r.right推进q 记录层次: 4. 初始化设置i 0; 5. 在入队的时候,入队一个对象{r: root, i} 6. 出队时,使用es6的解…

Redis在windows下安装过程(转载)

转载自&#xff08;http://www.cnblogs.com/M-LittleBird/p/5902850.html&#xff09; 一、下载windows版本的Redis 官网以及没有下载地址&#xff0c;只能在github上下载&#xff0c;官网只提供linux版本的下载 官网下载地址&#xff1a;http://redis.io/download github下载地…

C# Socket网络编程精华篇

我们在讲解Socket编程前&#xff0c;先看几个和Socket编程紧密相关的概念&#xff1a; TCP/IP层次模型当然这里我们只讨论重要的四层 01&#xff0c;应用层(Application)&#xff1a;应用层是个很广泛的概念&#xff0c;有一些基本相同的系统级TCP/IP应用以及应用协议&#xff…

Luogu1443 马的遍历【STL通俗BFS】

喜闻乐见当做BFS的STL模板做了 qwq我这样的蒟蒻也就只能发发模板题 #include<cstdio> #include<cstring> #include<cmath> #include<queue> using namespace std; struct xy{int x,y; }node,top; int dx[8]{1,1,2,2,-1,-1,-2,-2}; int dy[8]{2,-2,1,-1…

javascript --- [虚拟DOM] 初始化 实现

说明 本篇主要说明为什么要使用虚拟DOM技术,以及如何实现简单的虚拟dom您将会学到: 1.原生JS对DOM的操作 2.虚拟DOM的相关概念 3.DIFF算法的基础概念 为什么提出 -> DOM操作慢 我们使用createElement属性来创建一个最常见的div,看看一个最常见的DOM有多少个属性 <scri…

模块单元学习笔记(日志记录模块os模块sys)

一、日志记录模块 Logging 默认情况下&#xff0c;logging将日志打印到屏幕&#xff0c;日志级别大小关系为&#xff1a;CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET&#xff0c;当然也可以自己定义日志级别。 DEBUG&#xff1a;详细的信息,通常只出现…

webpack --- [4.x]你能看懂的webpack项目初始化

说明: 本篇文章主要做如下事情: 创建一个基本的webpack4.x 项目[报错]: The mode option has not been set, webpack will fallback to production for this value[报错]: ERROR in Entry module not found: Error: Can not resolve ./src in D:\L-react\HeiMa\01.webpack-ba…

tomcat8 进入不了Manager App 界面 403 Access Denied

准备 1.注释掉context.xml中的value属性 使用下面的命令&#xff1a; vim /usr/local/tomcats/tomcat-daily/webapps/manager/META-INF/context.xml 注释掉其中value节点 2.修改tomcat-users.xml文件 加入下面的配置 <role rolename"manager-gui" /><role …

SCRIPT70: 没有权限

主要原因&#xff1a;iframe安全而引发的问题&#xff0c;浏览器中js是没有垮域访问的权限的。如果用到iframe首先确保不垮域&#xff0c;或者不用iframe以绕开这个问题。 另外在jquery的早期版本中如&#xff1a;jquery-1.9.1.js $(#iframeWeb).attr(src, url);出现这样的问题…

webpack --- 在项目中使用React

说明: 分为2步: 首先导入react 和 react-dom:保证了虚拟DOM的创建和使用使用babel转码器: 由于DOM结构太多,每次使用React.createElement创建虚拟DOM会给开发带来很大压力,因此采用html的写法,通过babel转码器转换成React语法,可以很大程度上提高开发效率 项目源代码 在项目…

js改变select下拉框默认选择的option

比较简单&#xff0c;记录一下 var obj document.getElementById("fun"); obj.options[0].selected true; 转载于:https://www.cnblogs.com/vicF/p/9844028.html

vue拦截器实现统一token,并兼容IE9验证

项目中使用vue搭建前端页面&#xff0c;并通过axios请求后台api接口&#xff0c;完成数据交互。如果验证口令token写在在每次的接口中&#xff0c;也是个不小的体力活&#xff0c;而且也不灵活。这里分享使用vue自带拦截器&#xff0c;给每次请求的头部添加token&#xff0c;而…

Android Studio --- [学习笔记]Button、TextView、EditText

说明 源代码为了更全面的了解RN,先熟悉一下Android开发 第1章 Android 初体验 1.1 Android开发概述 Android是Google开发的操作系统Android开发是移动应用开发的表现形式之一(Android、IOS、H5 App、Native H5、 RN、ionic、MUI…) 1.2 Android开发工具 Android Studio为…

BZOJ2154: Crash的数字表格 BZOJ2693: jzptab

【传送门&#xff1a;BZOJ2154&BZOJ2693】 简要题意&#xff1a; 给出n,m&#xff0c;求$\sum_{i1}^{n}\sum_{j1}^{m}LCM(i,j)$ 题解&#xff1a; 莫比乌斯反演&#xff08;因为BZOJ2693是多组数据&#xff0c;数据强一点&#xff0c;所以代码用BZOJ2693的&#xff09; 设n…