数据库左连接、右连接、内连接、全连接区别
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/p1ibuRQ0oDliabZkxCTNIZjFbX3iaIPyeY0ePKJP9DfSTbmJRTibExcnnUxuIPDDKwlBh0yB5qljUh5fWzj7ibQibcSg/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></strong></span></strong></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">基本定义: </h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">left join (左连接):返回<span style="color: black;">包含</span>左表中的所有记录和右表中连接字段相等的记录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">right join (右连接):返回<span style="color: black;">包含</span>右表中的所有记录和左表中连接字段相等的记录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">inner join (等值连接<span style="color: black;">或</span>叫内连接):只返回两个表中连接字段相等的行。</p>full join (全外连接):返回左右表中所有的记录和<span style="color: black;">上下</span>表中连接字段相等的记录。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1、内联接</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (典型的联接运算,<span style="color: black;">运用</span>像 = 或 <> 之类的比较运算符)。<span style="color: black;">包含</span>相等联接和自然联接。 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 内联接<span style="color: black;">运用</span>比较运算符<span style="color: black;">按照</span><span style="color: black;">每一个</span>表共有的列的值匹配两个表中的行。例如,检索 students和courses表中学生标识号相同的所有行。 </p><span style="color: black;"><strong style="color: blue;">2、外联接</strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外联接<span style="color: black;">能够</span>是左向外联接、右向外联接或完整<span style="color: black;">外边</span>联接。</p> 在 FROM子句中指定外联接时,<span style="color: black;">能够</span>由下列几组关键字中的一组指定:
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1)LEFT JOIN或LEFT OUTER JOIN </p>左向外联接的结果集<span style="color: black;">包含</span> LEFT OUTER子句中指定的左表的所有行,而<span style="color: black;">不仅</span>是联接列所匹配的行。<span style="color: black;">倘若</span>左表的某行在右表中<span style="color: black;">无</span>匹配行,则在<span style="color: black;">关联</span>联的结果集行中右表的所有<span style="color: black;">选取</span>列表列均为空值。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2)RIGHT JOIN 或 RIGHT OUTER JOIN </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 右向外联接是左向外联接的反向联接。将返回右表的所有行。<span style="color: black;">倘若</span>右表的某行在左表中<span style="color: black;">无</span>匹配行,则将为左表返回空值。 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3)FULL JOIN 或 FULL OUTER JOIN</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完整<span style="color: black;">外边</span>联接返回左表和右表中的所有行。当某行在另一个表中<span style="color: black;">无</span>匹配行时,则另一个表的<span style="color: black;">选取</span>列表列<span style="color: black;">包括</span>空值。<span style="color: black;">倘若</span>表之间有匹配行,则<span style="color: black;">全部</span>结果集行<span style="color: black;">包括</span>基表的数据值。</p><span style="color: black;"><strong style="color: blue;">3、交叉联接 </strong> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接<span style="color: black;">亦</span><span style="color: black;">叫作</span>作笛卡尔积。 </p>FROM 子句中的表或视图可<span style="color: black;">经过</span>内联接或完整<span style="color: black;">外边</span>联接按任意<span style="color: black;">次序</span>指定;<span style="color: black;">然则</span>,用左或右向外联接指定表或视图时,表或视图的<span style="color: black;">次序</span>很重要。<span style="color: black;">相关</span><span style="color: black;">运用</span>左或右向外联接<span style="color: black;">摆列</span>表的<span style="color: black;">更加多</span>信息,请参见<span style="color: black;">运用</span>外联接。
<h2 style="color: black; text-align: left; margin-bottom: 10px;">示例:</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> A表 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> id name </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1 小王</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 小李</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3 小刘</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> B表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> id A_id job</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1 2 老师</p> 2 4 程序员
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内连接:(<span style="color: black;">仅有</span>2张表匹配的行<span style="color: black;">才可</span><span style="color: black;">表示</span>)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select a.name,b.job from A a inner join B b on a.id=b.A_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 只能得到一条记录</p> 小李 老师
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左连接:(左边的表不加限制)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select a.name,b.job from A a left join B b on a.id=b.A_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 三条记录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">小王 null</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 小李 老师</p> 小刘 null
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">右连接:(右边的表不加限制)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select a.name,b.job from A a right join B b on a.id=b.A_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 两条记录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 小李 老师</p> null 程序员
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">全外连接:(<span style="color: black;">上下</span>2张表都不加限制)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select a.name,b.job from A a full join B b on a.id=b.A_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 四条数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 小王 null</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 小李 老师</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 小刘 null</p> null 程序员
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1aibbpBosgRvwaloPWF0Srrm20GBCFdjMgm3ZfCHYENOAKdA4xUibrp1PA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">注:</strong>在sql中l外连接<span style="color: black;">包含</span>左连接(left join )和右连接(right join),全外连接(full join),等值连接(inner join)又叫内连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Ljib4So7yuWiazPia6ZCuiaRGZ8r5Cd84HYEr3FqwNCZEmZUlrHXicYnR0IFSl57XXVXFOl2ibYFRvdjXJ6bm8sJXia2w/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Bye~</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/p1ibuRQ0oDlhQf0uXMiaFJLFgbUFAZrG1at6ibbYlLQibCVZpRBfpmdFiapiaTRlic9icENEYyhZpvqbjjfibBzZ7fbOoyw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不想<span style="color: black;">错失</span>就把<span style="color: black;">咱们</span></span><strong style="color: blue;">设为星标</strong><span style="color: black;">吧!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">第1</span>步:点击顶部蓝字<strong style="color: blue;">“自动化软件测试”</strong>,进入公众号主页</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第二步:点击右上角<strong style="color: blue;">“···”</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第三步:点击<span style="color: black;"><strong style="color: blue;">“设为星标”</strong></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/p1ibuRQ0oDlgLsOcVfbG5N0ENSxh9yF24bONLDoVRCPpXNhVGWOfPeMqGEtichqytMApoevdrIAvebh3iaXD7l76A/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
同意、说得对、没错、我也是这么想的等。 这篇文章真的让我受益匪浅,外链发布感谢分享! 论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/ 论坛外链网http://www.fok120.com/
页:
[1]