MySQL:联合查询(2)

首先写一个三个表的联合查询

查询所有同学的每门课成绩,及同学的个人信息

1.我们首先要确定使用哪些表

学生表,课程表,成绩表

2.取笛卡尔积

 select * from score,student,course;

3. 确定表与表之间的联合条件

select * from score,student,course where score.student_id=student.student_id and course.course_id=score.course_id;

 

4.确定过滤条件(无)

5.精简查询字段

 select student.student_id,student.sn,score.score,course.name from score,student,course where score.student_id=student.student_id and course.course_id=score.course_id;

外连接

外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完全显示我们就说是右外连接。
使用前
select * from student,class  where student.class_id=class.class_id;

 

右外连接: 

 select * from student right join class on student.class_id=class.class_id;

 

右外连接,是以join 右边的表为基准,这个标的数据会全部的显示出来,左边的表没有与之匹配的记录全部会用null去填充。

左外连接:

 select * from student left join class on student.class_id=class.class_id;

 

查询哪些同学没有成绩

 select * from student left join score on student.student_id=score.student_id;

 都可以用来找出没有成绩的人

select * from student left join score on student.student_id=score.student_id where score_id is null;

select * from student left join score on student.student_id=score.student_id where score.score is null;

自连接

原理:自己和自己的表进行连接

注:可以把行转为列,通过where进行过滤,进行行与行之间的比较。

显示所有的“计算机原理”成绩比“Java”成绩高的成绩信息

 

比较相同的表时,要将表给与别名,否则无法比较

 select * from score sc1,score sc2;

判断连接的条件

select * from score sc1,score sc2 where sc1.student_id=sc2.student_id;

 找出同时有计算机原理和Java的信息

select * from score sc1,score sc2 where sc1.student_id=sc2.student_id and sc1.course_id=3 and sc2.course_id=1;

 找出计算机原理比Java成绩高的

select * from score sc1,score sc2 where sc1.student_id=sc2.student_id and sc1.course_id=3 and sc2.course_id=1 and sc1.score>sc2.score;

子查询

也叫嵌套查询,子查询是指嵌入在其他sql语句中的select语句。

select * from table where table_id=(select id from table2 where  ......)

单行子查询 

查询"不想毕业同学"的同班同学

1.参与查询的表

学生表

2.找到这个同学的班级编号

 select class_id from student where name='不想毕业';

3.找出与"不想毕业"同学班级编号相同的同学

 select student_id,name,class_id from student where class_id=1;

4.使用子查询,通过嵌套查询

select student_id,name,class_id from student where class_id=(select class_id from student where name='不想毕业');

5.除去这个同学

select student_id,name,class_id from student where class_id=(select class_id from student where name='不想毕业') and name!='不想毕业';

多行子查询

查询语文或英语的课程成绩信息

1.需要的表

课程表,成绩表

2.获取语文和英语的课程编号

select name,course_id from course where name='语文'or name ='英文';

3.根据获得的编号在成绩表中查询成绩

 select * from score where course_id=4 or course_id=6;

4.将上述的分布查询并起来,成子查询

select * from score where course_id in(select course_id from course where name='语文' or name ='英文');

注:如果查询不包括语文和英文的课程成绩信息

select * from score where course_id not in(select course_id from course where name='语文' or name ='英文');

查询重复的分数

 select * from score where (student_id,course_id,score)
    -> in (select student_id,course_id,score from score group by student_id,course_id,score having count(*) >1);

 

[NOT] EXISTS关键字:

exists 后面的括号中的查询语句,如果有结果返回,则执行外层的查询,如果返回是一个空结果集,则不执行外层的查询(相当于if()语句,进行判断,有结果返回true,无结果返回false)

select * from student where exists(select * from student where student_id=1);

如果exists内返回是空结果集,则不执行外层查询 

 

查询所有比中文系20193平均分高的成绩信息

1.获得"中文系2019级3班的平均分"

  1. 先从班级中根据班级名找到班级编号
  2. 根据班级编号在学生表中找到班里的学生及学生编号
  3. 根据学生的编号在成绩表中计算出平均分
select avg(score.score) from score,student,class where class.class_id=student.class_id and student.student_id=score.student_id and class.name='中文系2019级3班';

 2.再用表中的真实成绩和上表的平均值做比较

 select * from score,( select avg(score.score) score from score,student,class where class.class_id=student.class_id and student.student_id=score.stude
nt_id and class.name='中文系2019级3班') tmp where score.score>tmp.score;

合并查询 

(UNUIN)   (UNION  all)

原理:将多个查询结果合并到一个结果集中

 

将两个表合并

select * from student where student_id=1 union  select * from student where name= '许仙';

 注:

在单表中推荐用or去连接不同的条件

再多表中,就无法用or,如果最终的结果是从多个表中获得到的,必须用union进行合并

union和union all的差别 

1.根据一张表的结构创建新表

create table student2 like student;

2.将新表中添加几个数据

 

通过union把student表和student2表并在一起

通过union  all把student表和student2表并在一起

我们可以看出union合并时会去掉重复行,union all 不会去掉重复行

上述讲解用到的几个表

DROP TABLE IF EXISTS `score`;
DROP TABLE IF EXISTS `student`;
DROP TABLE IF EXISTS `class`;
DROP TABLE IF EXISTS `course`;
 
 
 
 
CREATE TABLE `class`  (
  `class_id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  PRIMARY KEY (`class_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
 
 
 
INSERT INTO `class` VALUES (1, '计算机系2019级1班');
INSERT INTO `class` VALUES (2, '中文系2019级3班');
INSERT INTO `class` VALUES (3, '自动化2019级5班');
 
 
 
 
CREATE TABLE `course`  (
  `course_id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  PRIMARY KEY (`course_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
 
 
 
INSERT INTO `course` VALUES (1, 'Java');
INSERT INTO `course` VALUES (2, '中国传统文化');
INSERT INTO `course` VALUES (3, '计算机原理');
INSERT INTO `course` VALUES (4, '语文');
INSERT INTO `course` VALUES (5, '高阶数学');
INSERT INTO `course` VALUES (6, '英文');
 
 
 
 
CREATE TABLE `student`  (
  `student_id` bigint NOT NULL AUTO_INCREMENT,
  `sn` varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `mail` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `class_id` bigint NULL DEFAULT NULL,
  PRIMARY KEY (`student_id`) USING BTREE,
  UNIQUE INDEX `sn`(`sn` ASC) USING BTREE,
  INDEX `class_id`(`class_id` ASC) USING BTREE,
  CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`class_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
 
 
INSERT INTO `student` VALUES (1, '09982', '黑旋风李逵', 'xuanfeng@qq.com', 1);
INSERT INTO `student` VALUES (2, '00835', '菩提老祖', NULL, 1);
INSERT INTO `student` VALUES (3, '00391', '白素贞', NULL, 1);
INSERT INTO `student` VALUES (4, '00031', '许仙', 'xuxian@qq.com', 1);
INSERT INTO `student` VALUES (5, '00054', '不想毕业', NULL, 1);
INSERT INTO `student` VALUES (6, '51234', '好好说话', 'say@qq.com', 2);
INSERT INTO `student` VALUES (7, '83223', 'tellme', NULL, 2);
INSERT INTO `student` VALUES (8, '09527', '老外学中文', 'foreigner@qq.com', 2);
 
 
 
CREATE TABLE `score`  (
  `score_id` bigint NOT NULL AUTO_INCREMENT,
  `student_id` bigint NULL DEFAULT NULL,
  `course_id` bigint NULL DEFAULT NULL,
  `score` decimal(5, 2) NULL DEFAULT NULL,
  PRIMARY KEY (`score_id`) USING BTREE,
  INDEX `student_id`(`student_id` ASC) USING BTREE,
  INDEX `course_id`(`course_id` ASC) USING BTREE,
  CONSTRAINT `score_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `score_ibfk_2` FOREIGN KEY (`course_id`) REFERENCES `course` (`course_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
 
INSERT INTO `score` VALUES (1, 1, 1, 70.50);
INSERT INTO `score` VALUES (2, 1, 3, 98.50);
INSERT INTO `score` VALUES (3, 1, 5, 33.00);
INSERT INTO `score` VALUES (4, 1, 6, 98.00);
INSERT INTO `score` VALUES (5, 2, 1, 60.00);
INSERT INTO `score` VALUES (6, 2, 5, 59.50);
INSERT INTO `score` VALUES (7, 3, 1, 33.00);
INSERT INTO `score` VALUES (8, 3, 3, 68.00);
INSERT INTO `score` VALUES (9, 3, 5, 99.00);
INSERT INTO `score` VALUES (10, 4, 1, 67.00);
INSERT INTO `score` VALUES (11, 4, 3, 23.00);
INSERT INTO `score` VALUES (12, 4, 5, 56.00);
INSERT INTO `score` VALUES (13, 4, 6, 72.00);
INSERT INTO `score` VALUES (14, 5, 1, 81.00);
INSERT INTO `score` VALUES (15, 5, 5, 37.00);
INSERT INTO `score` VALUES (16, 6, 2, 56.00);
INSERT INTO `score` VALUES (17, 6, 4, 43.00);
INSERT INTO `score` VALUES (18, 6, 6, 79.00);
INSERT INTO `score` VALUES (19, 7, 2, 80.00);
INSERT INTO `score` VALUES (20, 7, 6, 92.00);

 

 希望能对大家有所帮助!!!!

 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【leetcode】704. 二分查找

注意一般mid left (right-left)/2; 不要用mid (right - left)/2 中间值的计算需要考虑到整型溢出的问题。 如果使用 mid (right - left) / 2 的方式计算中间值,那么在 right 和 left 的值接近极限值的情况下,可能会导致计算出的中间值发生整型溢出&…

RHCE的练习(12)

写一个脚本,完成以下要求: 给定一个用户: 如果其UID为0,就显示此为管理员;否则,就显示其为普通用户; #!/bin/bash ​ # 使用read命令获取用户名 read -p "请输入用户名: " username ​…

WPF-控件的属性值的类型转化

控件的属性值需要转成int、double进行运算的&#xff0c;可以使用一下方法 页面代码 <StackPanel Margin"4,0,0,0" Style"{StaticResource Form-StackPanel}"> <Label Content"替换后材料增加金额&#xff…

【从零开始的LeetCode-算法】3270. 求出数字答案

给你三个 正 整数 num1 &#xff0c;num2 和 num3 。 数字 num1 &#xff0c;num2 和 num3 的数字答案 key 是一个四位数&#xff0c;定义如下&#xff1a; 一开始&#xff0c;如果有数字 少于 四位数&#xff0c;给它补 前导 0 。答案 key 的第 i 个数位&#xff08;1 < …

iMetaOmics | 刘永鑫/陈同-用于食物微生物组成和时间序列研究的微生物组数据库FoodMicroDB...

点击蓝字 关注我们 FoodMicroDB&#xff1a;用于食物微生物组成和时间序列研究的微生物组数据库 iMeta主页&#xff1a;http://www.imeta.science 研究论文 ● 原文链接DOI: https://doi.org/10.1002/imo2.40 ● 2024年11月1日&#xff0c;中国农业科学院深圳农业基因组研究所刘…

视觉slam十四讲 ch8 光流法和直接法

之前的都是单层光流 转载至Blibli 视觉SLAM十四讲_7视觉里程计1_计算相机运动_哔哩哔哩_bilibili

QSS 设置bug

问题描述&#xff1a; 在QWidget上add 一个QLabel&#xff0c;但是死活不生效 原因&#xff1a; c 主程序如下&#xff1a; QWidget* LOGO new QWidget(logo_wnd);LOGO->setFixedSize(logo_width, 41);LOGO->setObjectName("TittltLogo");QVBoxLayout* tit…

Linux运维篇-iscsi存储搭建

目录 概念实验介绍环境准备存储端软件安装使用targetcli来管理iSCSI共享存储 客户端软件安装连接存储 概念 iSCSI是一种在Internet协议上&#xff0c;特别是以太网上进行数据块传输的标准&#xff0c;它是一种基于IP Storage理论的存储技术&#xff0c;该技术是将存储行业广泛…

WSL--无需安装虚拟机和docker可以直接在Windows操作系统上使用Linux操作系统

安装WSL命令 管理员打开PowerShell或Windows命令提示符&#xff0c;输入wsl --install&#xff0c;然后回车 注意&#xff1a;此命令将启用运行 WSL 和安装 Linux 的 Ubuntu 发行版所需的功能。 注意&#xff1a;默认安装最新的Ubuntu发行版。 注意&#xff1a;默认安装路径是…

【学习心得】算力云平台上的大模型部署并实现远程调用

以AutoDL算力云平台为例&#xff0c;部署国产开源ChatGLM3b模型。 一、准备工作 &#xff08;1&#xff09;准备一台算力服务器 首先&#xff0c;进入AutoDL官网的算力时长选择算力服务器资源。 创建好后会自动跳转控制台的“容器实例”界面&#xff0c;稍等片刻后选择“快捷…

Vue 中的透传,插槽,依赖注入

1. 透传attributes 在组件上使用透传attribute&#xff1a; 当你在父组件中使用子组件时&#xff0c;你可以添加一些attribute到子组件上&#xff0c;即使这些attribute没有在子组件的props中声明。 父组件&#xff1a; <!-- 父组件&#xff0c;例如 ParentComponent.vue…

97.【C语言】数据结构之栈

目录 栈 1.基本概念 2.提炼要点 3.概念选择题 4.栈的实现 栈初始化函数 入栈函数 出栈函数和栈顶函数 栈顶函数 栈销毁函数 栈 基本概念参见王爽老师的《汇编语言 第四版》第56和57页 节选一部分 1.基本概念 注意:这里提到的数据结构中的栈有别于操作系统的栈,后者是…

Spring-boot 后端java配置接口返回jsp页面

Spring-boot 后端java配置接口返回jsp页面 spring boot 基于spring MVC的基础上进行了改进&#xff0c; 将Controller 与ResponseBody 进行了合并成一个新的注解 RestController。 当用户请求时&#xff0c;需要有视图渲染的&#xff0c;与请求数据的请求分别使用 1.在appli…

【操作系统实验课】Makefile与编译

1. 创建项目结构 my_project 使用mkdir命令在根目录下创建项目my_project sudo mkdir /my_project 进入my_project目录 cd my_project src 在my_project目录下创建src子目录 sudo mkdir src 进入src目录 cd src root(根用户) 切换用户身份为root(根用户) root用户…

冠层四流近似模型的发展历史

1. Kunbelka-Munk theory This is the earlist model using a two-stream approximation d I d z − ( k s ) I s J d J d z ( k s ) J − s I \begin{aligned} &\frac{dI}{dz} -(ks)IsJ\\ &\frac{dJ}{dz} (ks)J - sI \end{aligned} ​dzdI​−(ks)IsJdzdJ​(…

Linux从0——1之shell编程4

声明&#xff01; 学习视频来自B站up主 **泷羽sec** 有兴趣的师傅可以关注一下&#xff0c;如涉及侵权马上删除文章&#xff0c;笔记只是方便各位师傅的学习和探讨&#xff0c;文章所提到的网站以及内容&#xff0c;只做学习交流&#xff0c;其他均与本人以及泷羽sec团队无关&a…

2024.5 AAAiGLaM:通过邻域分区和生成子图编码对领域知识图谱对齐的大型语言模型进行微调

GLaM: Fine-Tuning Large Language Models for Domain Knowledge Graph Alignment via Neighborhood Partitioning and Generative Subgraph Encoding 问题 如何将特定领域知识图谱直接整合进大语言模型&#xff08;LLM&#xff09;的表示中&#xff0c;以提高其在图数据上自…

【大语言模型】ACL2024论文-15 大型语言模型中的最佳解释推断

【大语言模型】ACL2024论文-15 大型语言模型中的最佳解释推断 目录 文章目录 【大语言模型】ACL2024论文-15 大型语言模型中的最佳解释推断目录摘要研究背景问题与挑战如何解决创新点算法模型实验效果推荐阅读指数&#xff1a;★★★★☆后记 大型语言模型中的最佳解释推断 摘…

【最新鸿蒙开发之性能优化——动态加载和延迟加载】

大家好&#xff0c;我是学徒小z&#xff0c;在经历了一段时间项目开发中&#xff0c;我也渐渐意识到了性能的重要性&#xff0c;今天就分享一篇优化应用运行性能的文章&#xff0c;话不多说&#xff0c;开干&#xff01; 引言 延时触发操作与延迟加载的简介 动态加载&#x…

云计算研究实训室建设方案

一、引言 随着云计算技术的迅速发展和广泛应用&#xff0c;职业院校面临着培养云计算领域专业人才的迫切需求。本方案旨在构建一个先进的云计算研究实训室&#xff0c;为学生提供一个集理论学习、实践操作、技术研发与创新于一体的综合性学习平台&#xff0c;以促进云计算技术…