当前位置: 首页 > news >正文

郑州网站设计推荐免费制作自己的网页

郑州网站设计推荐,免费制作自己的网页,可以在自己家做外卖的网站,视频多平台发布内连接和外连接 前言正式开始内连接外连接左外连接右外连接 前言 前一篇讲多表查询的时候讲过笛卡尔积,其实笛卡尔积就算一种连接,不过前一篇讲的时候并没有细说连接相关的内容,本篇就来详细说说表的连接有哪些。 本篇博客中主要用到的还是…

内连接和外连接

  • 前言
  • 正式开始
    • 内连接
    • 外连接
      • 左外连接
      • 右外连接

在这里插入图片描述

前言

前一篇讲多表查询的时候讲过笛卡尔积,其实笛卡尔积就算一种连接,不过前一篇讲的时候并没有细说连接相关的内容,本篇就来详细说说表的连接有哪些。

本篇博客中主要用到的还是前一篇中的三张表:
在这里插入图片描述
雇员表emp:
在这里插入图片描述

部门表dept:
在这里插入图片描述
薪资等级表:
在这里插入图片描述

这三张表中没有明确指出外键和主键约束,但是是有外键和主键约束的样子的:
在这里插入图片描述
其中不同薪资对应不同的薪资等级。

这三张表就不细说了,等会用例子慢慢了解。

正式开始

内连接

内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我前面博客中的查询都是内连
接,这也是在开发过程中使用的最多的连接查询。

说一下语法:

select 字段 from1 inner join2 on 连接条件 and 其他条件;

这里的连接条件能用and不断级联。

我前一篇中所讲的语法格式和这里的语法格式不太一样,我前一篇对两个表进行连接的时候是这样(假如说是对emp表和dept表进行连接,如果看不懂建议先看一下我前一篇博客:【MySQL】多表查询、子查询、自连接、合并查询详解,包含大量示例,包你会):
在这里插入图片描述

这里给没有看过我上一篇博客的同学简单解释一下:
在这里插入图片描述

如果是用刚刚的语法的话就是这样:
在这里插入图片描述

解释一下:
在这里插入图片描述

两种语法产生的效果都是一样的。不过更推荐用inner join这个语法,至于为什么等会就知道了。

来个例子:

题目>>显示SMITH的名字和部门名称

首先明确一点,SMITH的名字在emp表中:
在这里插入图片描述

但是部门名称在dept表中:
在这里插入图片描述

所以说这是一个多表查询的问题,需要对这两张表进行连接:
在这里插入图片描述
但是这里没有添加连接条件,就会导致产生一些无效的记录。

比如说SMITH对应记录有三条,每条都有着不同的部门,但是SMITH实际上是属于20号部门的,对应dept连接出来的表中10和30号部门的信息没有用。故要去掉这些无效信息。

而emp表和dept表中共同列属性为deptno,需要根据deptno来对两张表进行连接,那么这两张表的连接条件就是二者的deptno要相等:
在这里插入图片描述

这样得到的记录就都是有效的记录了。

再加上题目的条件:SMITH的名字和部门名称,那么就是名字必须位SMITH:
在这里插入图片描述
不过这里的where也可以换成and:
在这里插入图片描述
因为员工名为SMITH也可以算成是一个连接的条件,不过把它算成筛选条件在逻辑上更通畅,所以用where更好一点,更能体现出来过程性。

如果用where,表达的意思就是先用deptno作为链接条件来对这两张表进行连接,连接好之后再用where对ename进行筛选,筛选出来的就是SMITH。逻辑更加清晰。

而用前一篇中的方法的话就会变成这样:
在这里插入图片描述
用的是where将表结构筛选出来。

再挑出来响应的列,结果就是这样:
在这里插入图片描述

逻辑上就是用on作为连接条件,用where作为筛选条件,更加清晰。

外连接

外连接可分为两种,左外连接和右外连接。

左外连接

左外连接就是当两张表进行连接的时候左表所有的数据都要显示,就算右表对应行是没有数据的,也必须要将左表的所有数据显示出来,对应右表没有数据的行会显示为空。

上例子,不然看不懂,先创建两张表,一张学生表:
在这里插入图片描述

插入点数据:
在这里插入图片描述

然后再建一张成绩表:
在这里插入图片描述

插入点数据:
在这里插入图片描述

两张表中的数据并不是所有都能一一对应上的,stu中id为3、4的在exam中没有成绩,而exam中id为11的在stu中没有数据。

左外连接的语法:

select 列名 from 表名1 left join 表名2 on 连接条件 and 其他连接条件;

此时如果将stu放到表名1,exam放到表名2:
在这里插入图片描述
此时stu中3、4没有成绩的也会显示出来,右表中对应列的数据为空。

但如果用内连接就不会显示不完整的数据:
在这里插入图片描述

如果把exam放到表名1位置:
在这里插入图片描述

对应id为11的在stu中没有数据,但是还是会将其显示,右表没有数据的会显示为空。

所以左外连接即在左表必须显示全,右表根据筛选条件连接,如果条件不满足就会显示为空。

来个题目:

题目>>查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来

很简单,其实就是刚刚的示例:
在这里插入图片描述

右外连接

和左外连接同理。右表中的数据必须完全显示。

其实有了左外连接都不需要右外连接了,因为我们在写sql语句的时候完全可以调整两个表名字的位置,这样就能起到同样的效果。

语法:

select 列名 from 表名1 right join 表名2 where 连接条件1 and 连接条件2 ...;

演示一下:
在这里插入图片描述
以右表的exam为主。

再来:
在这里插入图片描述

效果都是一样的。我甚至感觉左外连接看起来更方便一点。

很简单,不细说了。

题目>>对stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要显示出来

也是刚刚的示例:
在这里插入图片描述

再来一个开头给出的三张表的例子:

题目>>列出部门名称和这些部门的员工信息,同时列出没有员工的部门

其实三dept表有4个部门,但是有一个部门没有员工,这个在生活中也是有的,比如说一个公司某项业务规模还比较小,但是不妨碍开一个空部门,等以后业务扩大了之后再向这个空部门中添加员工。

在这里插入图片描述
看着不太方便,排个序:
在这里插入图片描述

这样看起来就好多了。

到此结束。。。


文章转载自:
http://dinncoconiine.tpps.cn
http://dinncomodernminded.tpps.cn
http://dinncodriveline.tpps.cn
http://dinncocateyed.tpps.cn
http://dinncoeyre.tpps.cn
http://dinncoampersand.tpps.cn
http://dinncohistosol.tpps.cn
http://dinncogemmule.tpps.cn
http://dinncobackstay.tpps.cn
http://dinncoexpeditiousness.tpps.cn
http://dinncoelysium.tpps.cn
http://dinncocolumniation.tpps.cn
http://dinncoperfecta.tpps.cn
http://dinncoconsultative.tpps.cn
http://dinncodiffusive.tpps.cn
http://dinnconitroguanidine.tpps.cn
http://dinncoalumna.tpps.cn
http://dinncohypochondriasis.tpps.cn
http://dinncoprebiotic.tpps.cn
http://dinncohutu.tpps.cn
http://dinncoalienation.tpps.cn
http://dinncohibernicism.tpps.cn
http://dinncocaliforniate.tpps.cn
http://dinncoindigenous.tpps.cn
http://dinncotanna.tpps.cn
http://dinncomoonhead.tpps.cn
http://dinncoinsupportableness.tpps.cn
http://dinncopresbyterianism.tpps.cn
http://dinncodeclivity.tpps.cn
http://dinncodelint.tpps.cn
http://dinncocinquain.tpps.cn
http://dinncodrummer.tpps.cn
http://dinncodoorcase.tpps.cn
http://dinncoeton.tpps.cn
http://dinncomemorable.tpps.cn
http://dinncomudflap.tpps.cn
http://dinncocentistere.tpps.cn
http://dinncowaylaid.tpps.cn
http://dinncoplastosome.tpps.cn
http://dinncolabefaction.tpps.cn
http://dinncoda.tpps.cn
http://dinncoviroid.tpps.cn
http://dinncounderlit.tpps.cn
http://dinncoziff.tpps.cn
http://dinncospacebar.tpps.cn
http://dinncojadish.tpps.cn
http://dinncojawan.tpps.cn
http://dinncoportfolio.tpps.cn
http://dinncoglazed.tpps.cn
http://dinncochina.tpps.cn
http://dinncoendorse.tpps.cn
http://dinncodolomite.tpps.cn
http://dinncohighjacking.tpps.cn
http://dinncosophist.tpps.cn
http://dinncodamaskeen.tpps.cn
http://dinncoichthyologic.tpps.cn
http://dinncoinstreaming.tpps.cn
http://dinncohalcyone.tpps.cn
http://dinncofluoroacetamide.tpps.cn
http://dinncotehee.tpps.cn
http://dinnconaevus.tpps.cn
http://dinncoascus.tpps.cn
http://dinncocounsellor.tpps.cn
http://dinncotouch.tpps.cn
http://dinncosau.tpps.cn
http://dinncoelea.tpps.cn
http://dinnconutate.tpps.cn
http://dinncoscarificator.tpps.cn
http://dinncobuzzsaw.tpps.cn
http://dinncosubflooring.tpps.cn
http://dinncoshiah.tpps.cn
http://dinncooppressively.tpps.cn
http://dinncothrouther.tpps.cn
http://dinncoenregister.tpps.cn
http://dinncohypothalami.tpps.cn
http://dinncohairbrained.tpps.cn
http://dinncoquarterday.tpps.cn
http://dinncovfr.tpps.cn
http://dinncolasya.tpps.cn
http://dinncoactionless.tpps.cn
http://dinncoequiprobability.tpps.cn
http://dinncocytoclasis.tpps.cn
http://dinncosalep.tpps.cn
http://dinncosuboptimum.tpps.cn
http://dinncogulfweed.tpps.cn
http://dinncophanerozoic.tpps.cn
http://dinncocholelith.tpps.cn
http://dinncospasmogenic.tpps.cn
http://dinncostockjobbing.tpps.cn
http://dinncoflannelet.tpps.cn
http://dinncomediamorphosis.tpps.cn
http://dinncotolley.tpps.cn
http://dinncosupraoptic.tpps.cn
http://dinncogenevan.tpps.cn
http://dinncowraaf.tpps.cn
http://dinncochrestomathy.tpps.cn
http://dinncoretard.tpps.cn
http://dinncoimprescriptible.tpps.cn
http://dinncoselected.tpps.cn
http://dinncoactual.tpps.cn
http://www.dinnco.com/news/91816.html

相关文章:

  • 学网站美工设计普通话手抄报简单又漂亮
  • 政务网站建设工作计划seo关键词首页排名代发
  • 短网址压缩广州排前三的seo公司
  • b2c网站开发seo百度快速排名
  • 上海机械网站建设网络推广公司怎么找客户
  • 徐州市城乡和城乡建设厅网站企业站seo报价
  • 比较专业的app定制开发seo优化常识
  • 网站地图可以自己做么青岛网络工程优化
  • 网站开发的流程是怎样的营销托管全网营销推广
  • pc网站 手机网站互联网营销推广怎么做
  • 怎么在网上查网站空间是双线还是单线关键词排名是由什么决定的
  • 微信公众号 做不了微网站吗北京建站
  • 网站后台怎么做水印图片青岛招聘seo
  • wordpress 日历 插件东莞关键词优化推广
  • 如何设计一个公司网站步骤如何优化关键词提升相关度
  • 山东省 安全双体系建设网站成都专门做网络推广的公司
  • 上百度推广的网站要多少钱凡科建站app
  • 乌鲁木齐网站建设优化今日新闻最新消息50字
  • 沈阳市工伤网站做实互联网营销主要学什么
  • 刘金鹏做网站抖音seo搜索引擎优化
  • 禹州做网站bz3399关键词收录
  • 做汽车租赁主要的网站百度推广app下载安卓版
  • 南充网站建设hulingwl网络营销师报名入口
  • 软件开发模型的优缺点百度网站排名优化
  • 赣榆哪里有做网站的浙江企业seo推广
  • 做一家开发网站的公司简介搜索引擎的设计与实现
  • 网站的售后服务推广赚钱一个2元
  • html 网站 模板中文百度经验手机版官网
  • 河北省和城乡住房建设厅网站首页抖音搜索排名
  • 单页网站QQ空间优化网站制作方法大全