f9yx0du 发表于 2024-8-4 14:58:01

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&nbsp;- 选课记录表</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,<span style="color: black;">学生表&nbsp;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;">#&nbsp;学生表:student</span><span style="color: black;">create</span>&nbsp;<span style="color: black;">table</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;student</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">id</span>&nbsp;&nbsp;&nbsp;<span style="color: black;">int</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">not</span>&nbsp;<span style="color: black;">null</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary&nbsp;<span style="color: black;">key</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">name</span>&nbsp;<span style="color: black;">varchar</span>(<span style="color: black;">255</span>)&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;age&nbsp;&nbsp;<span style="color: black;">int</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">comment</span>&nbsp;<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;">#&nbsp;选课记录表:record</span><span style="color: black;">create</span>&nbsp;<span style="color: black;">table</span>&nbsp;<span style="color: black;">record</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">id</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">int</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">not</span>&nbsp;<span style="color: black;">null</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary&nbsp;<span style="color: black;">key</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">name</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">varchar</span>(<span style="color: black;">255</span>)&nbsp;<span style="color: black;">not</span>&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;student_id&nbsp;<span style="color: black;">int</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">not</span>&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">time</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datetime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">comment</span>&nbsp;<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;">#&nbsp;学生表数据</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;">#&nbsp;记录表数据</p>2021001,语文,1,2021<span style="color: black;">-01-18</span>&nbsp;15<span style="color: black;">:32</span><span style="color: black;">:47</span>2021002,数学,2,2021<span style="color: black;">-01-18</span>&nbsp;15<span style="color: black;">:33</span><span style="color: black;">:41</span>2021003,英语,3,2021<span style="color: black;">-01-18</span>&nbsp;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>&nbsp;15<span style="color: black;">:34</span><span style="color: black;">:33</span>2021005,体育,5,2021<span style="color: black;">-01-18</span>&nbsp;15<span style="color: black;">:34</span><span style="color: black;">:47</span>2021006,化学,8,2021<span style="color: black;">-01-18</span>&nbsp;15<span style="color: black;">:35</span><span style="color: black;">:12</span>2021007,生物,9,2021<span style="color: black;">-01-18</span>&nbsp;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&nbsp;<span style="color: black;">查找</span>的&nbsp;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;">&nbsp;和</span><span style="color: black;">&nbsp;DELETE&nbsp;</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;">#&nbsp;子<span style="color: black;">查找</span></span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;<span style="color: black;">where</span>&nbsp;<span style="color: black;">id</span>&nbsp;<span style="color: black;">in</span>&nbsp;(<span style="color: black;">select</span>&nbsp;student_id&nbsp;<span style="color: black;">from</span>&nbsp;<span style="color: black;">record</span>&nbsp;<span style="color: black;">where</span>&nbsp;student_id&lt;=<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;">#&nbsp;子<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>&nbsp;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>关键字&nbsp;<span style="color: black;">inner join</span>&nbsp;连接两张表,只返回满足&nbsp;<span style="color: black;">on</span>&nbsp;<span style="color: black;">要求</span>的,两张表的交集数据</p><span style="color: black;">#&nbsp;内连接</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;s&nbsp;<span style="color: black;">inner</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;r&nbsp;<span style="color: black;">on</span>&nbsp;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;">#&nbsp;内连接<span style="color: black;">查找</span>结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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&nbsp;外连接</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;">&nbsp;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>关键&nbsp;<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;">#&nbsp;左外连接</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;s&nbsp;<span style="color: black;">left</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;r&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;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;">#&nbsp;左外连接结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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;">#&nbsp;右外连接</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;s&nbsp;<span style="color: black;">right</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;r&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;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;">#&nbsp;右外连接结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>关键字&nbsp;<span style="color: black;">full join</span>连接两张表,返回左表和右表的所有数据,并<span style="color: black;">运用</span> null 值填充缺失的数据</p><span style="color: black;">#&nbsp;完全连接</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;s&nbsp;<span style="color: black;">full</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;r&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;s.id&nbsp;=&nbsp;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&nbsp;并不支持完全连接,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>左连接 + union +&nbsp;右连接的方式去模拟完全连接</span></p><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;<span style="color: black;">left</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;student.id&nbsp;=&nbsp;record.student_id</p><span style="color: black;">union</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;student&nbsp;<span style="color: black;">right</span>&nbsp;<span style="color: black;">join</span>&nbsp;<span style="color: black;">record</span>&nbsp;<span style="color: black;">on</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;student.id&nbsp;=&nbsp;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;">#&nbsp;完全连接结果</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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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>关键字&nbsp;<span style="color: black;">cross join</span>&nbsp;连接两张表进行<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;">#&nbsp;交叉连接</span><span style="color: black;">select</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;xag.student&nbsp;<span style="color: black;">as</span>&nbsp;s&nbsp;<span style="color: black;">cross</span>&nbsp;<span style="color: black;">join</span>&nbsp;xag.record&nbsp;<span style="color: black;">as</span>&nbsp;r&nbsp;<span style="color: black;">where</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;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;">#&nbsp;交叉连接结果</p>1,张三,18,2021001,语文,1,2021<span style="color: black;">-01-18</span>&nbsp;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>&nbsp;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>&nbsp;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>&nbsp;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>




b1gc8v 发表于 2024-10-1 20:04:15

谷歌外贸网站优化技术。

4zhvml8 发表于 2024-10-24 10:16:22

楼主的文章深得我心,表示由衷的感谢!

1fy07h 发表于 2024-11-12 17:43:06

太棒了、厉害、为你打call、点赞、非常精彩等。

m5k1umn 发表于 2024-11-14 14:07:45

你的言辞如同繁星闪烁,点亮了我心中的夜空。
页: [1]
查看完整版本: SQL 多表查询仔细讲解