数据库内连接以及上下连接的区别
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">在数据库中,<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>介绍内连接,左连接以及右连接的区别。</strong></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;">首要</span>,<span style="color: black;">咱们</span>来看一下内连接,左连接以及右连接的定义。</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">内连接</strong></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>的记录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">左连接</strong></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>的记录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">右连接</strong></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>的记录。</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;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">经过</span>例子<span style="color: black;">来讲</span>明内连接,左连接以及右连接的区别。</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">例</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">#新建一个数据库,名字叫lianxi</strong></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;">create database lianxi default CHARACTER set utf8;</span></strong></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;">选取</span>要<span style="color: black;">运用</span>的数据库lianxi</strong></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;">use lianxi;</span></strong></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;">兴趣</span>表(个人编号,个人姓名,个人<span style="color: black;">兴趣</span>)</strong></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;">create table habit</span></strong></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;">(</span></strong></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;">id int,</span></strong></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;">name varchar(20),</span></strong></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;">ah varchar(20)</span></strong></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;">);</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">#新建一张宠物表(个人编号,个人姓名,养的宠物)</strong></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;">create table pet</span></strong></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;">(</span></strong></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;">id int,</span></strong></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;">name varchar(20),</span></strong></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;">cw varchar(20)</span></strong></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;">)</span></strong></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;">兴趣</span>表<span style="color: black;">插进</span>三条数据</strong></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;">insert into habit values(1,"zhang1",”fishing”);</span></strong></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;">insert into habit values(2,"zhang2",”swimming”);</span></strong></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;">insert into habit values(3<span style="color: black;">"zhang2",”running”);</span></span></strong></p><img src="https://mmbiz.qpic.cn/mmbiz_png/a6oGj0jibl5DgIQuyCf6GoibeQJYKdq8zyS0CyUmia2ctib7YmPXav4MHmulCNLXGF7brA7lCDrTibpqe9IUbOM9lZw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">#给宠物表<span style="color: black;">插进</span>三条数据</strong></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;">insert into pet values(1,"zhang1",”pig”);</span></strong></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;">insert into pet values(2,"zhang2",”dog”);</span></strong></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;">insert into pet values(3,"li3",”duck”);</span></strong></p><img src="https://mmbiz.qpic.cn/mmbiz_png/a6oGj0jibl5DgIQuyCf6GoibeQJYKdq8zyevvFQictibRSnxPk8ric6Bca4qcLpoc3rtDycTMFqaicicUkmJGEI9d56nQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">内连接:在结果集中只<span style="color: black;">保存</span>各个表中满足匹配<span style="color: black;">要求</span>的记录</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT a.name,a.cw,b.ah from pet a join habit b on a.name=b.name;</p><img src="https://mmbiz.qpic.cn/mmbiz_png/a6oGj0jibl5DgIQuyCf6GoibeQJYKdq8zyeib9GrDxPg6E0F3N8jOZWePUcgibibTNuL9mic2Z2CAXQ4yibia2Qg109Viag/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在pet表和habit表中name字段<span style="color: black;">能够</span><span style="color: black;">相关</span>起来的<span style="color: black;">仅有</span>zhang1和zhang2</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>结果中只展示zhang1和zhang2的name,cw和ah的数据。</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;">保存</span>左表中的<span style="color: black;">所有</span>记录及右表中满足匹配<span style="color: black;">要求</span>的记录。</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT a.name,a.cw,b.ah from pet a left join habit b on a.name=b.name;</p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/a6oGj0jibl5DgIQuyCf6GoibeQJYKdq8zyWAF5HiayyfO1fHgubM2RKIJ1D0TeH9Ver9YwADxQRhUEricC7F5ibmlGw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> pet表是左表,habit是右表,展示pet表中的所有name和cw两个字段</span><span style="color: black;">的值,右表habit只展示zhang1和zhang2的ah的值</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;">保存</span>右表中的<span style="color: black;">所有</span>记录及左表中满足匹配<span style="color: black;">要求</span>的记录。</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">S</span><span style="color: black;">ELECT a.name,a.cw,b.ah from pet a right join habit b on a.name=b.name;</span></p><img src="https://mmbiz.qpic.cn/mmbiz_png/a6oGj0jibl5DgIQuyCf6GoibeQJYKdq8zy3fZAKSibc7m8famyMR0HsaBjRsF7htZVHSxMmXc1e1n76IPM73thtbA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><span style="color: black;"> pet表是左表,habit是右表,展示右表habit中ah字段的所有值,左表pet只展示zhang1和zhang2的name和cw字段的值</span>
我赞同你的看法,你的智慧让人佩服,谢谢分享。 我们有着相似的经历,你的感受我深有体会。 我完全同意你的观点,说得太对了。
页:
[1]