二、数据库查询语句(多表查询篇)

news2024/10/6 23:19:04

二、数据库查询语句(多表查询篇)

1、笛卡尔积

​ 前面涉及的都是单张表的查询,如果我们的查询条件相对比较复杂,需要涉及多张表进行查询,如果是两张无关的表联合查询,列出所有的可能的结果,如下图:

在这里插入图片描述

  • 如果没有连接条件,则是以左表为驱动表,从左表的第一条数据开始和右表的每一条数据相拼接组成的集合,叫做笛卡尔积,如上图共有4种情况,但我们实际需要的只是两条
  • 没有连接条件时,我们必须列举所有的可能性,就会产生上边的一张大表,如果两个表的数据量变大,比如每张表1000条数据,那笛卡尔积,就会扩张到1百万,如果是三张表关联,就必须再乘以1000,只要是多表连接,就会涉及到笛卡尔积,所以笛卡尔积是一个很恐怖的事情,我们要依据情况加上合适的连接条件,过滤掉我们不需要的数据。
  • 多表连接的方式主要分为:
    1. 交叉连接、自然连接、JOIN…USING、JOIN…ON
    2. 自连接、内连接、外连接

2、多表连接

该篇所需要的数据在文章最底部。


1.CROSS JOIN

简介:CROSS JOIN又称交叉连接,对连接的两张表记录做笛卡尔集,产生最终结果输出,分为无过滤条件和带过滤两种

  • 语法:select xxx from 模式.表1 cross join 模式.表2
  • 无过滤条件:student表和scores表做交叉连接

    • 代码:

      -- 交叉连接
      -- 10条数据 42 条数据
      select count(*) from other.student;
      select count(*) from other.scores;
      
      -- 无过滤条件,结果是420条数据
      select count(*) from other.student cross join other.scores;
      select count(*) from other.student,other.scores;
      
      -- 带过滤条件,结果是42条数据
      select count(*) from other.student t cross join other.scores s where t.id = s.s_id;
      
    • 效果图:在这里插入图片描述


2.NATURAL JOIN

简介:NATURAL JOIN又称为自然连接,自然连接会自动把两种表的同名列进行连接条件,进行等值连接,我们称这样的连接为自然连接。

自然连接具有以下特点:

  1. 连接表中存在同名列
  2. 如果有多个同名列,则会产生多个等值连接条件
  3. 如果连接表中的同名列类型不匹配,则报错处理
  • 语法:select xxx from 模式.表1 natural join 模式.表2
  • 代码:

    -- 自然连接
    create table other.t1(id int,name varchar(32));
    create table other.t2(id int,age int);
    insert into other.t1 values(1,'张三');
    insert into other.t1 values(2,'李四');
    insert into other.t1 values(3,'王五');
    insert into other.t2 values(2,18);
    insert into other.t2 values(3,20);
    commit;
    
    select * from other.t1 natural join other.t2;
    
  • 效果图:在这里插入图片描述


3.JOIN…USING

简介:这是自然连接的另一种写法,JOIN 关键字指定连接的两张表,USING 指明连接列。要求USING 中的列存在于两张连接表中

  • 语法:select xxx from 模式.表1 join 模式.表2 using(列名)
  • 代码:

    select * from other.t1 join other.t2 using(id);
    
  • 效果图:在这里插入图片描述


4.JOIN…ON

简介:这是一种连接查询的常用写法,说明是一个连接查询。JOIN 关键字指定连接的两张表, ON 子句指定连接条件表达式,其中不允许出现 ROWNUM。具体采用何种连接方式,由数据库内部分析确定

  • 语法:select xxx from 模式.表1 join 模式.表2 on 连接条件
  • 代码:查询学生的姓名和对应的成绩

    -- 查询学生的姓名和对应的成绩
    select st.name,s.score from other.student st join other.scores s on st.id = s.s_id;
    
  • 效果图:在这里插入图片描述


5.自连接

简介:数据表与自身进行连接,我们称这种连接为自连接。 自连接查询至少要对一张表起别名,否则,服务器无法识别要处理的是哪张表,太过简单,不做演示

  • 语法:select xxx from 模式.表1 别名, 模式.表2 别名
select * from other.t1 m,other.t2 m where m.id = n.id;

6.内连接

简介:根据连接条件,结果集仅包含满足全部连接条件的记录,我们称这样的连接为内连接,与上述join on相同,对于【内连接】中的两个表,若【驱动表】中的记录在【被驱动表】中找不到与之匹配的记录,则该记录不会被加入到最后的结果集中

  • 语法:select xxx from 模式.表1 inner join 模式.表2 on 连接条件

     通俗讲就是根据条件,找到表 A 和 表 B 的数据的交集。

在这里插入图片描述

  • 代码:下列三条sql语句等效

    -- 查询学生的姓名和对应的成绩
    select st.name,s.score from other.student st inner join other.scores s on st.id = s.s_id;
    select st.name,s.score from other.student st join other.scores s on st.id = s.s_id;
    select st.name,s.score from other.student st,other.scores s where st.id = s.s_id;
    
  • 效果图:在这里插入图片描述


7.外连接(常用)

简介:外连接对结果集进行了扩展,会返回一张表的所有记录,对于另一张表无法匹配的字段 用 NULL 填充返回。

DM 数据库支持三种方式的外连接:左外连接、右外连接、全外连接。

外连接中常用到的术语:左表、右表。根据表所在外连接中的位置来确定,位于左侧的 表,称为左表;位于右侧的表,称为右表。例如 SELECT * FROM T1 LEFT JOIN T2 ON T1.C1=T2.D1,T1 表为左表,T2 表为右表,对于【外连接】中的两个表,即使【驱动表】中的记录在【被驱动表】中找不到与之匹配的记录,也要将该记录加入到最后的结果集中,针对不同的【驱动表的选择】,又可以将外连接分为【左外连接】和【右外连接】

  • 语法:select xxx from 模式.表1 left/right join 模式.表2 on 连接条件

    我们可以使用一个图形来形容左外连接的效果:

在这里插入图片描述

  • 代码:下面两条语句等效

    -- 外连接
    -- 查询学生的姓名和对应的成绩
    select st.name,s.score from other.student st left outer join other.scores s on st.id = s.s_id;
    select st.name,s.score from other.student st left join other.scores s on st.id = s.s_id;
    
  • 效果图:在这里插入图片描述


8.总结

内连接和外连接的区别:

  • 对于【内连接】中的两个表,若【驱动表】中的记录在【被驱动表】中找不到与之匹配的记录,则该记录不会被加入到最后的结果集中。
  • 对于【外连接】中的两个表,即使【驱动表】中的记录在【被驱动表】中找不到与之匹配的记录,也要将该记录加入到最后的结果集中,针对不同的【驱动表的选择】,又可以将外连接分为【左外连接】和【右外连接】。

所以我们可以得出以下结论:

  • 对于左外连接查询的结果会包含左表的所有数据
  • 对于右外连接查询的结果会包含右表的所有数据

外连接的关键字是【outer join】 也可以省略outter,连接条件一样需要使用【on】关键字;


3、所需数据

学习之前我们需要创建数据库并填充部分数据:

drop TABLE if EXISTS student;
CREATE TABLE other.student (
    id INT PRIMARY key,
    name VARCHAR (10),
    age INT  NOT NULL,
    gender varchar(2)
);
drop TABLE if EXISTS course;
CREATE TABLE other.course (
  id INT   PRIMARY key,
  name VARCHAR  ,
  t_id INT  
) ;
drop TABLE if EXISTS teacher;
CREATE TABLE other.teacher(
  id INT   PRIMARY key,
  name VARCHAR  
);
drop TABLE if EXISTS scores;
CREATE TABLE other.scores(
  s_id INT ,
  score INT ,
  c_id INT,
	PRIMARY key(s_id,c_id)
) ;
commit;

表单填充数据:

insert into  other.student (id,name,age,gender)VALUES(1,'白杰',19,'男'),(2,'连宇栋',19,'男'),(3,'邸志伟',24,'男'),(4,'李兴',11,'男'),(5,'张琪',18,'男'),(6,'武三水',18,'女'),(7,'张志伟',16,'男'),(8,'康永亮',23,'男'),(9,'杨涛瑞',22,'女'),(10,'王杰',21,'男');

insert into  other.course (id,name,t_id)VALUES(1,'数学',1),(2,'语文',2),(3,'c++',3),(4,'java',4),(5,'php',null);


insert into  other.teacher (id,name)VALUES(1,'张楠'),(2,'李子豪'),(3,'薇薇姐'),(4,'猴哥'),(5,'八戒');


insert into  other.scores (s_id,score,c_id)VALUES(1,80,1);
insert into  other.scores (s_id,score,c_id)VALUES(1,56,2);
insert into  other.scores (s_id,score,c_id)VALUES(1,95,3);
insert into  other.scores (s_id,score,c_id)VALUES(1,30,4);
insert into  other.scores (s_id,score,c_id)VALUES(1,76,5);

insert into  other.scores (s_id,score,c_id)VALUES(2,35,1);
insert into  other.scores (s_id,score,c_id)VALUES(2,86,2);
insert into  other.scores (s_id,score,c_id)VALUES(2,45,3);
insert into  other.scores (s_id,score,c_id)VALUES(2,94,4);
insert into  other.scores (s_id,score,c_id)VALUES(2,79,5);

insert into  other.scores (s_id,score,c_id)VALUES(3,65,2);
insert into  other.scores (s_id,score,c_id)VALUES(3,85,3);
insert into  other.scores (s_id,score,c_id)VALUES(3,37,4);
insert into  other.scores (s_id,score,c_id)VALUES(3,79,5);

insert into  other.scores (s_id,score,c_id)VALUES(4,66,1);
insert into  other.scores (s_id,score,c_id)VALUES(4,39,2);
insert into  other.scores (s_id,score,c_id)VALUES(4,85,3);

insert into  other.scores (s_id,score,c_id)VALUES(5,66,2);
insert into  other.scores (s_id,score,c_id)VALUES(5,89,3);
insert into  other.scores (s_id,score,c_id)VALUES(5,74,4);


insert into  other.scores (s_id,score,c_id)VALUES(6,80,1);
insert into  other.scores (s_id,score,c_id)VALUES(6,56,2);
insert into  other.scores (s_id,score,c_id)VALUES(6,95,3);
insert into  other.scores (s_id,score,c_id)VALUES(6,30,4);
insert into  other.scores (s_id,score,c_id)VALUES(6,76,5);

insert into  other.scores (s_id,score,c_id)VALUES(7,35,1);
insert into  other.scores (s_id,score,c_id)VALUES(7,86,2);
insert into  other.scores (s_id,score,c_id)VALUES(7,45,3);
insert into  other.scores (s_id,score,c_id)VALUES(7,94,4);
insert into  other.scores (s_id,score,c_id)VALUES(7,79,5);

insert into  other.scores (s_id,score,c_id)VALUES(8,65,2);
insert into  other.scores (s_id,score,c_id)VALUES(8,85,3);
insert into  other.scores (s_id,score,c_id)VALUES(8,37,4);
insert into  other.scores (s_id,score,c_id)VALUES(8,79,5);

insert into  other.scores (s_id,score,c_id)VALUES(9,66,1);
insert into  other.scores (s_id,score,c_id)VALUES(9,39,2);
insert into  other.scores (s_id,score,c_id)VALUES(9,85,3);
insert into  other.scores (s_id,score,c_id)VALUES(9,79,5);

insert into  other.scores (s_id,score,c_id)VALUES(10,66,2);
insert into  other.scores (s_id,score,c_id)VALUES(10,89,3);
insert into  other.scores (s_id,score,c_id)VALUES(10,74,4);
insert into  other.scores (s_id,score,c_id)VALUES(10,79,5);
commit;

查看创建的表:

select * FROM OTHER.student;
select * FROM OTHER.scores;
select * FROM OTHER.course;
select * FROM OTHER.teacher;

在这里插入图片描述

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

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

相关文章

网易云课堂-课程分析

需要原始数据的可以联系我、在评论区留下邮箱 需要原始数据的可以联系我、在评论区留下邮箱 需要原始数据的可以联系我、在评论区留下邮箱 需要原始数据的可以联系我、在评论区留下邮箱 数据展示 一级类目 二级类目 三级类目 求和项:在学人数 平均值项:原始价格 平均值…

docker-compose配合Dockerfile使用

也就是在dockers-compose.yml文件中添加build 指定一下我的Dockerfile文件的路径 例如我的dockers-compose.yml文件在docker-compose文件夹下,而docker-compose文件夹与Dockerfile和项目的war包在同一级目录,也就是Dockerfile文件,在dockers-…

readme.md编写并生成html

目录1、Markdown教程2、生成html3、目录制作3.1 vscode Markdown Preview Enhanced 插件3.2 自定义侧边栏4、参考1、Markdown教程 Markdown 教程 | 菜鸟教程 2、生成html 通过 vscode 下载 Markdown Preview Enhanced、Markdown PDF等插件,就可以实现转化 3、目…

Linux网络管理OSI和TCP/IP

作者简介:一名软件运维工作人员,正在自学云计算课程。宣言:人生就是B(birth)和D(death)之间的C(choise),做好每一个选择。创作不易,动动小手给个点…

【改进灰狼优化算法】贪婪的非分层灰狼优化算法(Matlab代码实现)

👨‍🎓个人主页:研学社的博客 💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜…

RF电路设计常见bug及解决方法

单片射频器件大大方便了一定范围内无线通信领域的应用,采用合适的微控制器和天线并结合此收发器件即可构成完整的无线通信链路。它们可以集成在一块很小的电路板上,应用于无线数字音频、数字视频数据传输系统,无线遥控和遥测系统,…

转自【AI科技评论】专访李海洲教授 | 机器智能对话是毕生所求

一直来到实叻坡,乜事无。上山来做工,伯公“多隆”保平安。——潮州过番歌 所谓“过番”,指早期潮州人外出务工,乘坐小船历经七天七夜到达东南亚谋生,“实叻坡”是马来语“Selat”的音译词,便是指“新加坡”…

Quartz学习

任务执行流程 StdSchedulerFactory创建和属性初始化 如果自定义了属性,会在这里加载 StdScheduler创建 入口为StdSchedulerFactory#getScheduler();,首次进入时调用StdSchedulerFactory#instantiate: 如果没有配置自定义属性,则先…

2022年企业数字化技术应用 5 大趋势丨三叠云

根据易观分析发布的相关报告,本期视频将对2022年企业数字化技术应用 5 大趋势进行讲解,内容可能比较硬核,值得你先收藏再观看。 趋势一:武装数字员工“RPA低代码AI” 中国市场技术供应商正在快速推动技术民主化进程,其…

并发编程(二)有序性

【问题的产生】: 程序真的是按照顺序执行的吗? /*** 本程序跟可见性无关,曾经有同学用单核也发现了这一点*/import java.util.concurrent.CountDownLatch;public class T01_Disorder {private static int x 0, y 0;private static int a …

java小技能:集成开发工具(IDE)

文章目录 I IDEA1.1 下载1.2 试用II 忽略IntelliJ IDEA 文件2.1 .gitignore的例子2.2 从idea进行忽略III idea使用非模式提交界面IV DataGrip4.1安装4.2 Actsee alsoI IDEA 1.1 下载 https://www.jetbrains.com/zh-cn/idea/download/other.html 1.2 试用 IntelliJ IDEA 2021…

快手如何玩转复杂场景下的说话人识别?| ASRU 2021

快手是一个短视频社区,短视频和直播中通常混合各种形式的声音,如语音、音乐、特效音和背景噪声等,这些声音很好的提升了短视频和直播的用户消费体验,但同时也为音频内容理解带来极大的困难和挑战。如何在复杂场景下准确高效的进行…

AMS的启动

AMS的启动 Launcher请求AMS阶段 AMS到ApplicationThread阶段 ApplicationThread到Activity阶段 API28重构之后,ApplicationThread到Activity阶段 应用程序启动涉及的进程间通信 根Activity启动过程涉及到的进程之间的关系 根Activity启动过程中的进程调用时序图 A…

HTML期末大作业:基于HTML+CSS+JavaScript新能源汽车资讯门户网站

🎉精彩专栏推荐 💭文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 💂 作者主页: 【主页——🚀获取更多优质源码】 🎓 web前端期末大作业: 【📚毕设项目精品实战案例 (10…

兼容模式怎么设置?5个常用浏览器的设置方法

在使用电脑浏览器时,有时需要切换到兼容模式才能打开相应界面。许多浏览器现在都有自己的防病毒功能,这可能会直接将许多组件作为病毒屏蔽,导致某些元素无法在正常模式下显示。但是常用浏览器的兼容模式怎么设置呢?接下来让我们一…

【附源码】计算机毕业设计JAVA研究生入学考试备考辅助系统

【附源码】计算机毕业设计JAVA研究生入学考试备考辅助系统 目运行 环境项配置: Jdk1.8 Tomcat8.5 Mysql HBuilderX(Webstorm也行) Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。 项目技术:…

网络编程——socket定义和地址格式

网络编程——socket定义和地址格式 目录 socket 是什么?套接字地址格式 1. socket 是什么? 网络编程中, socket 翻译为套接字或套接口,指可以通过插口接入的方式,快速完成网络连接和数据收发。上图表示网络编程中&…

智能终端测试解决方案

概述 随着互联网产业的蓬勃发展,智能终端的崛起,无论是移动终端制造商、移动通信运营商以及移动互联网内容服务商,都面临着新技术、新业务、新服务不断涌现,商用进程不断加快的局面。 当前基于5G核心技术的移动传输网络已开发出…

只因简历上有“精通”Redis,阿里三面被面试官狂问 Redis,再也不敢乱写了

Redis 在国内各大公司都很热门,比如新浪、阿里、腾讯、百度、美团、小米等。Redis 也是大厂面试最爱问的,尤其是 Redis 客户端、Redis 高级功能、Redis 持久化和开发运维常用问题探讨、Redis 复制的原理和优化策略、Redis 分布式解决方案等。 Redis 我们…

Python 字符串详解

一、字符串概念 用于保存字符信息的数据模型(容器)。 1、只能存放一个值 2、不可变类型 3、有序,索引从0开始顺序访问 字符串语法格式: str1 “字符串信息” str2 字符串信息 str3 字符串信息 str4 字符串信息 二、字符串常用操作 1、字符串…