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)…

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

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

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

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

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

思路 使用队列: 初始化的时候,将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…

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;详细的信息,通常只出现…

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 …

MySQL中varchar最大长度是多少

一. varchar存储规则&#xff1a; 4.0版本以下&#xff0c;varchar(20)&#xff0c;指的是20字节&#xff0c;如果存放UTF8汉字时&#xff0c;只能存6个&#xff08;每个汉字3字节&#xff09; 5.0版本以上&#xff0c;varchar(20)&#xff0c;指的是20字符&#xff0c;无论存放…

salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)

在salesforce的classic中&#xff0c;我们使用{!expresion}在前台页面展示信息&#xff0c;在lightning中&#xff0c;上一篇我们也提及了&#xff0c;如果展示attribute的值&#xff0c;可以使用{!v.expresion}展示信息。 lightning在component中解析动态值的时候&#xff0c;…

网络协议各层概述

网络协议概述 OSI是一个开放性的通信系统互连参考模型&#xff0c;他是一个定义得非常好的协议规范。OSI模型有7层结构&#xff0c;每层都可以有几个子层。 OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层&#xff1b; 其中高层&…

javascript --- 实现对象的深拷贝

浅拷贝和深拷贝 浅拷贝: 只拷贝一层.当对象是复杂数据类型(Object、 Array)时,只拷贝引用深拷贝: 多层拷贝.复杂数据类型,会重新分配内存空间. 实现浅拷贝的2种方法 使用for ... in 实现 var obj {name: marron,age: 18,msg: {sex: "1" } } var o {}; for(let …

Qt与FFmpeg联合开发指南(二)——解码(2):封装和界面设计

与解码相关的主要代码在上一篇博客中已经做了介绍&#xff0c;本篇我们会先讨论一下如何控制解码速度再提供一个我个人的封装思路。最后回归到界面设计环节重点看一下如何保证播放器界面在缩放和拖动的过程中保证视频画面的宽高比例。 一、解码速度 播放器播放媒体文件的时候播…

Bzoj1051 受欢迎的牛

每一头牛的愿望就是变成一头最受欢迎的牛。现在有 N 头牛&#xff0c;给你 M 对整数 (A,B)&#xff0c;表示牛 A 认为牛 B 受欢迎。这种关系是具有传递性的&#xff0c;如果 A 认为 B 受欢迎&#xff0c;B 认为 C 受欢迎&#xff0c;那么牛 A 也认为牛 C 受欢迎。你的任务是求出…

javascript --- 文件上传即时预览 闭包实现多图片即时预览

使用javascript原生功能实现,点击上传文件,然后再网页上显示出来 1. 初级显示 1.1 准备一个input标签和一个img标签 <input typefile id"file"> <img id"preview" src"">1.2 js代码如下 // 将上传的图片显示到页面上function sho…

第一次作业:深入Linux源码分析进程模型

一.进程的概念 第一&#xff0c;进程是一个实体。每一个进程都有它自己的地址空间&#xff0c;一般情况下&#xff0c;包括文本区域&#xff08;text region&#xff09;、数据区域&#xff08;data region&#xff09;和堆栈&#xff08;stack region&#xff09;。文本区域存…

关于模型验证那点事儿

今天应笑笑老师之问&#xff0c;做了一个模型验证的例子&#xff0c;发现之前对这个东西的理解太片面&#xff0c;重新整理了一下思路 字段验证优先级高于类验证 什么是类验证呢&#xff1f;就是两个字段组合的验证&#xff0c;比如你Admin不允许修改密码&#xff0c;你修改密码…

Win10安装MySQL5.7.22 解压缩版(手动配置)方法

1.下载地址&#xff1a;https://dev.mysql.com/downloads/mysql/5.7.html#downloads 直接点击下载项 下载后&#xff1a; 2.可以把解压的内容随便放到一个目录&#xff0c;我的是如下目录&#xff08;放到C盘的话&#xff0c;可能在修改ini文件时涉及权限问题&#xff0c;之后我…

Elemant-UI日期范围的表单验证

Form 组件提供了表单验证的功能&#xff0c;只需要通过 rules 属性传入约定的验证规则&#xff0c;并将 Form-Item 的 prop 属性设置为需校验的字段名即可。但是官网的示例只有普通日期类型的验证&#xff0c;没有时间范围的验证。 一开始&#xff0c;我认为时间时间范围的是一…