悦之 發表於 2023-8-16 00:00:00

一篇文章带你了解数据库中JOIN的用法

<p>
        <span><strong>前言</strong></span></p>
<p>
        本章主要介绍数据库中join的的用法,也是我们在使用数据库时非常基础的一个知识点。本次会介绍数据库中的 inner join、 left join、 right join 的用法以及它们之间的区别。 文章如有错误还请大家及时指出~</p>
<blockquote>
        <p>
                以下都是采用mysql数据库</p>
</blockquote>
<p>
        <span><strong>join</strong></span></p>
<p>
        相信大家在学习数据库的使用时,都有使用过join,对数据库中的两张或两张以上表进行连接操作。</p>
<p>
        join 分为:</p>
<ul>
<li>
                内连接(inner join)</li>
        <li>
                外连接(outer join)</li>
</ul>
<p>
        其中外连接分为:</p>
<ul>
<li>
                左外连接(left outer join)</li>
        <li>
                右外连接(right outer join)</li>
        <li>
                全外连接(full outer join)</li>
</ul>
<p>
        说明:</p>
<blockquote>
        <p>
                1.其中外连接的“outer”关键字可以省略不写。 2.内连接用于返回满足连接条件的记录;而外连接则是内连接的扩展,它不仅会满足连接条件的记录,而且还会返回不满足连接条件的记录。</p>
</blockquote>
<p>
        <span><strong>笛卡尔积</strong></span></p>
<p>
        在我们进行多表联合查询的时候会出现的一种情况——笛卡尔积现象</p>
<p>
        我们以下面两张表举例:</p>
<p>
        学生表(student)</p>
<p>
         </p>
<table>
<thead><tr>
<th>
                                id</th>
                        <th>
                                studentname</th>
                        <th>
                                studentclassid</th>
                </tr></thead>
<tbody>
<tr>
<td>
                                1</td>
                        <td>
                                小明</td>
                        <td>
                                1</td>
                </tr>
<tr>
<td>
                                2</td>
                        <td>
                                小红</td>
                        <td>
                                2</td>
                </tr>
<tr>
<td>
                                3</td>
                        <td>
                                小兰</td>
                        <td>
                                3</td>
                </tr>
<tr>
<td>
                                4</td>
                        <td>
                                小吕</td>
                        <td>
                                2</td>
                </tr>
<tr>
<td>
                                5</td>
                        <td>
                                小梓</td>
                        <td>
                                1</td>
                </tr>
</tbody>
</table>
<p>
         </p>
<p>
        班级表(class)</p>
<p>
         </p>
<table>
<thead><tr>
<th>
                                classid</th>
                        <th>
                                classname</th>
                </tr></thead>
<tbody>
<tr>
<td>
                                1</td>
                        <td>
                                软件一班</td>
                </tr>
<tr>
<td>
                                2</td>
                        <td>
                                软件二班</td>
                </tr>
<tr>
<td>
                                3</td>
                        <td>
                                软件三班</td>
                </tr>
</tbody>
</table>
<p>
         </p>
<p>
        当我们进行查询操作的时候:</p>
<div class="jb51code">
        <div>
                <div class="syntaxhighlightersql" id="highlighter_21983">
                        <div class="toolbar">
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
                                                        <div class="line number1 index0 alt2">
                                                                1</div>
                                                </td>
                                                <td class="code">
                                                        <div class="container">
                                                                <div class="line number1 index0 alt2">
                                                                        <code class="sql keyword">select</code> <code class="sql plain">* </code><code class="sql keyword">from</code> <code class="sql plain">student,class;</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="一篇文章带你了解数据库中JOIN的用法" alt="一篇文章带你了解数据库中JOIN的用法" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/48197f817d284e0c74caf5ddb3c2b72a.jpg" style='font-size: 12px; border-top: 0px; font-family: "microsoft yahei", simsun, arial, sans-serif; border-right: 0px; white-space: normal; word-spacing: 0px; border-bottom: 0px; text-transform: none; font-weight: 400; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial'></p>
<p>
        就会出现上面的情况,也就是笛卡尔现象,表student中有5条记录,表class中有3条记录,那么对于表student而言有5种选择,对于表class来说有3种选择。所以一共有 5 * 3 = 15种选择了,也就是笛卡尔积。</p>
<p>
        <span><strong>内连接——inner join</strong></span></p>
<p>
        内连接查询返回满足条件的所有记录,默认情况下没有指定任何连接则为内连接。 例如:查询xx学生在xx班级 沿用上面的数据表</p>
<div class="jb51code">
        <div>
                <div class="syntaxhighlightersql" id="highlighter_897085">
                        <div class="toolbar">
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
                                                        <div class="line number1 index0 alt2">
                                                                1</div>
                                                </td>
                                                <td class="code">
                                                        <div class="container">
                                                                <div class="line number1 index0 alt2">
                                                                        <code class="sql keyword">select</code> <code class="sql plain">stu.studentname,cl.classname </code><code class="sql keyword">from</code> <code class="sql plain">student stu </code><code class="sql keyword">inner</code> <code class="sql color1">join</code> <code class="sql plain">class cl </code><code class="sql keyword">on</code> <code class="sql plain">stu.studentclassid=cl.classid;</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        查询结果</p>
<p>
        <img title="一篇文章带你了解数据库中JOIN的用法" alt="一篇文章带你了解数据库中JOIN的用法" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/3b7cdfff2e73b8a4102306c56dc8b92b.jpg" style='font-size: 12px; border-top: 0px; font-family: "microsoft yahei", simsun, arial, sans-serif; border-right: 0px; white-space: normal; word-spacing: 0px; border-bottom: 0px; text-transform: none; font-weight: 400; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial'></p>
<p>
        <span><strong>左外连接——left join</strong></span></p>
<p>
        左外连接查询不仅返回满足条件的所有记录,而且还会返回不满足连接条件的连接操作符左边表的其他行。 我们在原student 表中新增学生:小美 </p>
<p>
        <img title="一篇文章带你了解数据库中JOIN的用法" alt="一篇文章带你了解数据库中JOIN的用法" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/48b18e43723ea13a8712a7da9b397943.jpg" style='font-size: 12px; border-top: 0px; font-family: "microsoft yahei", simsun, arial, sans-serif; border-right: 0px; white-space: normal; word-spacing: 0px; border-bottom: 0px; text-transform: none; font-weight: 400; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial'></p>
<p>
        例如: 查询xx学生在xx班级 沿用上面的数据表</p>
<div class="jb51code">
        <div>
                <div class="syntaxhighlightersql" id="highlighter_368140">
                        <div class="toolbar">
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
                                                        <div class="line number1 index0 alt2">
                                                                1</div>
                                                </td>
                                                <td class="code">
                                                        <div class="container">
                                                                <div class="line number1 index0 alt2">
                                                                        <code class="sql keyword">select</code> <code class="sql plain">stu.studentname,cl.classname </code><code class="sql keyword">from</code> <code class="sql plain">student stu </code><code class="sql color2">left</code> <code class="sql color1">join</code> <code class="sql plain">class cl </code><code class="sql keyword">on</code> <code class="sql plain">stu.studentclassid=cl.classid;</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        查询结果</p>
<p>
        右外连接——right join<br>
        右外连接查询不仅返回满足条件的所有记录,而且还会返回不满足连接条件的连接操作符右边表的其他行。 我们在原class表中新增班级:</p>
<p>
        <img title="一篇文章带你了解数据库中JOIN的用法" alt="一篇文章带你了解数据库中JOIN的用法" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/f6146a1744b50aa9fe221c8efcfca87d.jpg" style='font-size: 12px; border-top: 0px; font-family: "microsoft yahei", simsun, arial, sans-serif; border-right: 0px; white-space: normal; word-spacing: 0px; border-bottom: 0px; text-transform: none; font-weight: 400; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial'></p>
<p>
        软件四班</p>
<p>
         例如: 查询xx学生在xx班级 沿用上面的数据表</p>
<div class="jb51code">
        <div>
                <div class="syntaxhighlightersql" id="highlighter_744759">
                        <div class="toolbar">
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
                                                        <div class="line number1 index0 alt2">
                                                                1</div>
                                                </td>
                                                <td class="code">
                                                        <div class="container">
                                                                <div class="line number1 index0 alt2">
                                                                        <code class="sql keyword">select</code> <code class="sql plain">stu.studentname,cl.classname </code><code class="sql keyword">from</code> <code class="sql plain">student stu </code><code class="sql color2">right</code> <code class="sql color1">join</code> <code class="sql plain">class cl </code><code class="sql keyword">on</code> <code class="sql plain">stu.studentclassid=cl.classid;</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        查询结果</p>
<p>
        <img title="一篇文章带你了解数据库中JOIN的用法" alt="一篇文章带你了解数据库中JOIN的用法" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/8c60ec86e7ec3799ca3d49f298fca981.jpg" style='font-size: 12px; border-top: 0px; font-family: "microsoft yahei", simsun, arial, sans-serif; border-right: 0px; white-space: normal; word-spacing: 0px; border-bottom: 0px; text-transform: none; font-weight: 400; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial'></p>
<p>
        <span><strong>全连接——full join</strong></span></p>
<p>
        全连接查询不仅返回满足条件的所有记录,而且还会返回不满足连接条件的其他行。</p>
<p>
        <span><strong>注:</strong></span>mysql默认不支持full join。</p>
<p>
        这里我们就不做介绍了。</p>
<p>
        <span><strong>文末</strong></span></p>
<p>
        本章节主要介绍了iterable与iterator之间的区别与联系,以及其他方面的小知识点,也是面试过程中会出现的内容点。</p>
<p>
        好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。</p>
<p>
        原文链接:https://mp.weixin.qq.com/s/9Q_xZR0uA4zKVi2aLWI7sw</p>
頁: [1]
查看完整版本: 一篇文章带你了解数据库中JOIN的用法