SQL 多表查询仔细讲解
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1. 前言</span></h2>
<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>方式,在<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>:<span style="color: black;">子<span style="color: black;">查找</span></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>将利用一个实例逐一介绍这些操作</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2. 准备</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以 Mysql 数据库为例,创建两张数据表:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">student - 学生表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">record - 选课记录表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,<span style="color: black;">学生表 id 字段对应选课记录表中的 student_id 字段</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Sql 如下:</span></p><span style="color: black;"># 学生表:student</span><span style="color: black;">create</span> <span style="color: black;">table</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> student</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p> <span style="color: black;">id</span> <span style="color: black;">int</span> <span style="color: black;">not</span> <span style="color: black;">null</span> primary <span style="color: black;">key</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">name</span> <span style="color: black;">varchar</span>(<span style="color: black;">255</span>) <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> age <span style="color: black;">int</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p> <span style="color: black;">comment</span> <span style="color: black;">学生表</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;"># 选课记录表:record</span><span style="color: black;">create</span> <span style="color: black;">table</span> <span style="color: black;">record</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p> <span style="color: black;">id</span> <span style="color: black;">int</span> <span style="color: black;">not</span> <span style="color: black;">null</span> primary <span style="color: black;">key</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">name</span> <span style="color: black;">varchar</span>(<span style="color: black;">255</span>) <span style="color: black;">not</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> student_id <span style="color: black;">int</span> <span style="color: black;">not</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">time</span> datetime <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p> <span style="color: black;">comment</span> <span style="color: black;">选课记录</span>
<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;">而后</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;"># 学生表数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1,张三,18</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2,李四,23</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3,王五,30</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4,马六,35</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5,孙七,40</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6,朱八,19</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">7,黄九,53</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 记录表数据</p>2021001,语文,1,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:32</span><span style="color: black;">:47</span>2021002,数学,2,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:33</span><span style="color: black;">:41</span>2021003,英语,3,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:01</span>2021004,<span style="color: black;">理学</span>,4,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:33</span>2021005,体育,5,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:47</span>2021006,化学,8,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:35</span><span style="color: black;">:12</span>2021007,生物,9,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:35</span><span style="color: black;">:39</span>2021008,音乐,10,2021<span style="color: black;">-01-18</span>15<span style="color: black;">:36</span><span style="color: black;">:00</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.1 子<span style="color: black;">查找</span></span></h2>
<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>,是一种嵌套在其他 Sql <span style="color: black;">查找</span>的 Where 子句中的<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>的数据;<span style="color: black;">子<span style="color: black;">查找</span><span style="color: black;">能够</span>用在</span><span style="color: black;">SELECT、INSERT、UPDATE</span><span style="color: black;"> 和</span><span style="color: black;"> DELETE </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>以 SELECT 语句为例,在两张表中<span style="color: black;">运用</span>子<span style="color: black;">查找</span>,筛选出满足条件的记录</p><span style="color: black;"># 子<span style="color: black;">查找</span></span><span style="color: black;">select</span> * <span style="color: black;">from</span> student <span style="color: black;">where</span> <span style="color: black;">id</span> <span style="color: black;">in</span> (<span style="color: black;">select</span> student_id <span style="color: black;">from</span> <span style="color: black;">record</span> <span style="color: black;">where</span> student_id<=<span style="color: black;">3</span>
<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;">查找</span>结果如下:</p>
<span style="color: black;"># 子<span style="color: black;">查找</span>的结果</span><span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,张三,18</p><span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,李四,23</p><span style="color: black;">3</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,王五,30)</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 style="color: black;">查找</span><span style="color: black;">必要</span><span style="color: black;">包括</span>在圆括号内,并且<span style="color: black;">不可</span><span style="color: black;">运用</span> ORDER BY 进行排序</span></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.2 内连接</span></h2>
<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;">inner join</span> 连接两张表,只返回满足 <span style="color: black;">on</span> <span style="color: black;">要求</span>的,两张表的交集数据</p><span style="color: black;"># 内连接</span><span style="color: black;">select</span> * <span style="color: black;">from</span> student s <span style="color: black;">inner</span> <span style="color: black;">join</span> <span style="color: black;">record</span> r <span style="color: black;">on</span> s.id=r.student_id;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:32</span><span style="color: black;">:47</span>2,李四,23,2021002,数学,2,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:33</span><span style="color: black;">:41</span>3,王五,30,2021003,英语,3,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:01</span>4,马六,35,2021004,<span style="color: black;">理学</span>,4,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:33</span>5,孙七,40,2021005,体育,5,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:47</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>的是,<span style="color: black;"><span style="color: black;">倘若</span>内连接<span style="color: black;">无</span><span style="color: black;">经过</span> on 关键字指定<span style="color: black;">要求</span>的话,<span style="color: black;">查找</span>结果和交叉连接<span style="color: black;">查找</span>结果<span style="color: black;">同样</span>,只是执行效率高于交叉连接</span></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.3 外连接</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接<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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">右外连接</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;">运用</span>关键字<span style="color: black;"> left join</span>,以左表为准,返回左表的所有数据,右表满足 on <span style="color: black;">要求</span>的数据会<span style="color: black;">所有</span><span style="color: black;">表示</span>,否则用 null 值去填充</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;">right join</span>,以右表为准,返回右表的所有数据,左表满足 on <span style="color: black;">要求</span>的数据会<span style="color: black;">所有</span><span style="color: black;">表示</span>,否则用 null 值去填充</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>
<span style="color: black;"># 左外连接</span><span style="color: black;">select</span> * <span style="color: black;">from</span> student s <span style="color: black;">left</span> <span style="color: black;">join</span> <span style="color: black;">record</span> r <span style="color: black;">on</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s.id=r.student_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>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:32</span><span style="color: black;">:47</span>2,李四,23,2021002,数学,2,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:33</span><span style="color: black;">:41</span>3,王五,30,2021003,英语,3,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:01</span>4,马六,35,2021004,<span style="color: black;">理学</span>,4,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:33</span>5,孙七,40,2021005,体育,5,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:47</span>6,朱八,19,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>7,黄九,53,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</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><span style="color: black;"># 右外连接</span><span style="color: black;">select</span> * <span style="color: black;">from</span> student s <span style="color: black;">right</span> <span style="color: black;">join</span> <span style="color: black;">record</span> r <span style="color: black;">on</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s.id=r.student_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>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:32</span><span style="color: black;">:47</span>2,李四,23,2021002,数学,2,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:33</span><span style="color: black;">:41</span>3,王五,30,2021003,英语,3,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:01</span>4,马六,35,2021004,<span style="color: black;">理学</span>,4,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:33</span>5,孙七,40,2021005,体育,5,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:47</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,2021006,化学,8,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:35</span><span style="color: black;">:12</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,2021007,生物,9,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:35</span><span style="color: black;">:39</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,2021008,音乐,10,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:36</span><span style="color: black;">:00</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.4 完全连接</span></h2>
<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;">full join</span>连接两张表,返回左表和右表的所有数据,并<span style="color: black;">运用</span> null 值填充缺失的数据</p><span style="color: black;"># 完全连接</span><span style="color: black;">select</span> * <span style="color: black;">from</span> student s <span style="color: black;">full</span> <span style="color: black;">join</span> <span style="color: black;">record</span> r <span style="color: black;">on</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s.id = r.student_id;</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;">Mysql 并不支持完全连接,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>左连接 + union + 右连接的方式去模拟完全连接</span></p><span style="color: black;">select</span> * <span style="color: black;">from</span> student <span style="color: black;">left</span> <span style="color: black;">join</span> <span style="color: black;">record</span> <span style="color: black;">on</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> student.id = record.student_id</p><span style="color: black;">union</span><span style="color: black;">select</span> * <span style="color: black;">from</span> student <span style="color: black;">right</span> <span style="color: black;">join</span> <span style="color: black;">record</span> <span style="color: black;">on</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> student.id = record.student_id;</p>
<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;"># 完全连接结果</span><span style="color: black;">1</span>,张三,<span style="color: black;">18</span>,<span style="color: black;">2021001</span>,语文,<span style="color: black;">1</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">32</span>:<span style="color: black;">47</span><span style="color: black;">2</span>,李四,<span style="color: black;">23</span>,<span style="color: black;">2021002</span>,数学,<span style="color: black;">2</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">33</span>:<span style="color: black;">41</span><span style="color: black;">3</span>,王五,<span style="color: black;">30</span>,<span style="color: black;">2021003</span>,英语,<span style="color: black;">3</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">34</span>:<span style="color: black;">01</span><span style="color: black;">4</span>,马六,<span style="color: black;">35</span>,<span style="color: black;">2021004</span>,<span style="color: black;">理学</span>,<span style="color: black;">4</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">34</span>:<span style="color: black;">33</span><span style="color: black;">5</span>,孙七,<span style="color: black;">40</span>,<span style="color: black;">2021005</span>,体育,<span style="color: black;">5</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">34</span>:<span style="color: black;">47</span><span style="color: black;">6</span>,朱八,<span style="color: black;">19</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span><span style="color: black;">7</span>,黄九,<span style="color: black;">53</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">2021006</span>,化学,<span style="color: black;">8</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">35</span>:<span style="color: black;">12</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">2021007</span>,生物,<span style="color: black;">9</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">35</span>:<span style="color: black;">39</span><span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">NULL</span>,<span style="color: black;">2021008</span>,音乐,<span style="color: black;">10</span>,<span style="color: black;">2021</span><span style="color: black;">-01</span><span style="color: black;">-18</span> <span style="color: black;">15</span>:<span style="color: black;">36</span>:<span style="color: black;">00</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.5 交叉连接</span></h2>
<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;">cross join</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> where 加入限制<span style="color: black;">要求</span>,则返回两张表行数的乘积;<span style="color: black;">倘若</span>加入限制<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>为例</p><span style="color: black;"># 交叉连接</span><span style="color: black;">select</span> * <span style="color: black;">from</span> xag.student <span style="color: black;">as</span> s <span style="color: black;">cross</span> <span style="color: black;">join</span> xag.record <span style="color: black;">as</span> r <span style="color: black;">where</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s.id=r.student_id;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span>结果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 交叉连接结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:32</span><span style="color: black;">:47</span>2,李四,23,2021002,数学,2,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:33</span><span style="color: black;">:41</span>3,王五,30,2021003,英语,3,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:01</span>4,马六,35,2021004,<span style="color: black;">理学</span>,4,2021<span style="color: black;">-01-18</span>15<span style="color: black;">:34</span><span style="color: black;">:33</span>5,孙七,40,2021005,体育,5,2021<span style="color: black;">-01-18</span> 15<span style="color: black;">:34</span><span style="color: black;">:47</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>的是,<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></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4. 最后</span></h2>
<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 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>
谷歌外贸网站优化技术。 楼主的文章深得我心,表示由衷的感谢! 太棒了、厉害、为你打call、点赞、非常精彩等。
页:
[1]