【数据库】SQL连接最全总结:加强你的数据库查询技能
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">把握</span> SQL 中的连接<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>这个关键 SQL 技能的关键。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/3049f212a9644581bd3499810b575dc1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347256&x-signature=iS14ak5YLDL6rl%2F4E3R8lbnIrLo%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SQL连接类型</p>
</div>
<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> SQL 连接的核心类型——</span><strong style="color: blue;"><span style="color: black;">内连接、左连接、右连接、全连接</span></strong><span style="color: black;">和</span><strong style="color: blue;"><span style="color: black;">交叉连接</span></strong><span style="color: black;">——每一种类型在数据合并中都有其独特的用途。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">SQL 中连接类型</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">内连接(Inner Join)</span></strong><span style="color: black;">:只返回两张表中满足匹配<span style="color: black;">要求</span>的记录。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">左连接(Left (Outer) Join)</span></strong><span style="color: black;">:返回左表的所有行以及右表中的匹配行,<span style="color: black;">针对</span>右表中未匹配上的行,其列值在结果集中用 NULL 填充。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">右连接(Right(Outer) Join)</span></strong><span style="color: black;">:与左连接类似,只是主表为右表。返回右表的所有行以及左表中的匹配行,<span style="color: black;">针对</span>左表中未匹配上的行,其列值在结果集中用 NULL 填充。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">全连接(Full (Outer) Join)</span></strong><span style="color: black;">:返回两个表的所有行(无论<span style="color: black;">是不是</span>匹配),<span style="color: black;">针对</span><span style="color: black;">上下</span>表中未匹配上的行,其列值在结果集中用 NULL 填充。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">自连接(Self Join)</span></strong><span style="color: black;">:一种独特的连接类型,其中表与<span style="color: black;">自己</span>连接。当你<span style="color: black;">必须</span>比较同一表中的行时,就适合用自连接。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">交叉连接(Cross Join)</span></strong><span style="color: black;">:<span style="color: black;">亦</span><span style="color: black;">叫作</span>为笛卡尔连接,返回两个表的笛卡尔积,这<span style="color: black;">寓意</span>着<span style="color: black;">第1</span>个表的每一行都与第二个表的所有行相结合。<span style="color: black;">例如</span>,若 A,B 两个表的行数分别为 m 和 n,则交叉连接后结果集中的总行数为:m * n。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">语法和示例</h1>
<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 style="color: black;">运用</span>示例。假设<span style="color: black;">咱们</span>在数据库中有两个表:Employees 和 Departments。Employees 表有 EmployeeID、Name 和 DeptID 列,而 Departments 表有 DeptID 和 DeptName 列。</span></p><strong style="color: blue;">内连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span> column1, column2, ...
<span style="color: black;">FROM</span> table1
<span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> table2
<span style="color: black;">ON</span> table1.match_column = table2.match_column;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span>Employees.Name, Departments.DeptName<span style="color: black;">FROM</span> Employees
<span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> Departments <span style="color: black;">ON</span> Employees.DeptID = Departments.DeptID;<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 style="color: black;">叫作</span>,但仅获取分配到<span style="color: black;">分部</span>的员工的姓名。</span></p><strong style="color: blue;">左连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span>column1, column2, ...<span style="color: black;">FROM</span> table1
<span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> table2
<span style="color: black;">ON</span> table1.match_column = table2.match_column;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span> Employees.Name, Departments.DeptName
<span style="color: black;">FROM</span> Employees
<span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span>Departments<span style="color: black;">ON</span> Employees.DeptID = Departments.DeptID;<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 style="color: black;">分部</span>的员工,在这种<span style="color: black;">状况</span>下,DeptName 列<span style="color: black;">表示</span>为 NULL。</span></p><strong style="color: blue;">右连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span> column1, column2, ...
<span style="color: black;">FROM</span> table1
<span style="color: black;">RIGHT</span> <span style="color: black;">JOIN</span> table2
<span style="color: black;">ON</span> table1.match_column = table2.match_column;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span> Employees.Name, Departments.DeptName
<span style="color: black;">FROM</span> Employees
<span style="color: black;">RIGHT</span> <span style="color: black;">JOIN</span> Departments <span style="color: black;">ON</span>Employees.DeptID = Departments.DeptID;<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 style="color: black;">包含</span><span style="color: black;">哪些</span><span style="color: black;">无</span>分配任何员工的<span style="color: black;">分部</span>,这些<span style="color: black;">分部</span>的 Name 列为 NULL。</span></p><strong style="color: blue;">全连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span> column1, column2, ...
<span style="color: black;">FROM</span> table1
<span style="color: black;">FULL</span> <span style="color: black;">OUTER</span> <span style="color: black;">JOIN</span> table2
<span style="color: black;">ON</span>table1.match_column = table2.match_column;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span> Employees.Name, Departments.DeptName
<span style="color: black;">FROM</span> Employees
<span style="color: black;">FULL</span> <span style="color: black;">OUTER</span> <span style="color: black;">JOIN</span> Departments <span style="color: black;">ON</span>Employees.DeptID = Departments.DeptID;<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 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></p><strong style="color: blue;">自连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span> column1, column2, ...
<span style="color: black;">FROM</span> table1 <span style="color: black;">AS</span> alias1
<span style="color: black;">JOIN</span> table1 <span style="color: black;">AS</span> alias2
<span style="color: black;">ON</span>alias1.match_column = alias2.match_column;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span> A.Name <span style="color: black;">AS</span> EmployeeName, B.Name <span style="color: black;">AS</span> ManagerName
<span style="color: black;">FROM</span> Employees A
<span style="color: black;">JOIN</span> Employees B
<span style="color: black;">ON</span> A.ManagerID = B.EmployeeID;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例说明:假设 Employees 表有一个 ManagerID 列引用经理的 EmployeeID,该<span style="color: black;">查找</span>将列出员工和<span style="color: black;">她们</span>的经理。</span></p><strong style="color: blue;">交叉连接语法及示例</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法:</span></p><span style="color: black;">SELECT</span> column1, column2, ...
<span style="color: black;">FROM</span> table1
<span style="color: black;">CROSS</span> <span style="color: black;">JOIN</span> table2;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p><span style="color: black;">SELECT</span>Employees.Name, Departments.DeptName<span style="color: black;">FROM</span> Employees
<span style="color: black;">CROSS</span> <span style="color: black;">JOIN</span> Departments;<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 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>笛卡尔积产生的行数量比较多,<span style="color: black;">因此</span>会影响<span style="color: black;">查找</span>性能(<span style="color: black;">尤其</span>是连接表的记录数较高时)。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">连接优化技术</h1>
<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> SQL <span style="color: black;">查找</span><span style="color: black;">有效</span>运行,请<span style="color: black;">思虑</span>以下优化技术:</span></p><span style="color: black;">索引:在连接<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><span style="color: black;">连接类型:<span style="color: black;">选取</span>适当的连接类型以<span style="color: black;">尽可能</span>减少返回的行数。</span><span style="color: black;">提前过滤:在连接之前应用 WHERE 子句以减小结果集的<span style="color: black;">体积</span>。</span><span style="color: black;">子<span style="color: black;">查找</span>最低优先级:在兼容的<span style="color: black;">状况</span>下,<span style="color: black;">尽可能</span><span style="color: black;">选择</span> EXISTS 或 IN 子句而非子<span style="color: black;">查找</span>。</span><span style="color: black;">连接<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><span style="color: black;">避免不必要的列:只<span style="color: black;">选取</span>必要的列以减<span style="color: black;">少许</span>据负载。</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">平常</span>的陷阱和<span style="color: black;">怎样</span>避免它们</h1>
<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 style="color: black;">平常</span>的陷阱:</span></p><span style="color: black;">笛卡尔积:<span style="color: black;">倘若</span>忘记 ON 子句,结果将会<span style="color: black;">引起</span>笛卡尔积,从而创建一个过大的结果集。</span><span style="color: black;">连接类型错误:<span style="color: black;">运用</span>错误的连接类型将会返回不期望的结果。</span><span style="color: black;">空值:在连接可能<span style="color: black;">包括</span>空值的列时要小心,<span style="color: black;">由于</span>它们可能会影响结果集。</span><span style="color: black;">性能问题:连接<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>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">结论</h1>
<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> SQL 中的连接<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>这个关键 SQL 技能的关键。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">--END--</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">欢迎关注【<strong style="color: blue;">辉哥传书vlog</strong>】头条号,<span style="color: black;"><span style="color: black;">爱好</span>记得点赞、<span style="color: black;">保藏</span>、评论、转发哦!</span></p>
顶楼主,说得太好了! 回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。 在遇到你之前,我对人世间是否有真正的圣人是怀疑的。 你的见解真是独到,让我受益匪浅。
页:
[1]