SQL多表连接 - 内连接INNER JOIN
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/tOgI4VrBDEnTZWg742QGZF3ojQCRcGsV0sI03w02qtMd5twoIAdljb2ZIltgeX4IcKXlLV3suKEoTMTfKfOqGg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">大众</span>好,我是林赛~</span>
<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右连接INNER JOIN。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SQL中的多表连接的语句<span style="color: black;">运用</span>JOIN关键字实现,用于将多个表中的<span style="color: black;">查找</span>结果进行组合。多表连接有四种连接类型,分别为LEFT JOIN、RIGHT JOIN、INNER JOIN、FULL JOIN。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在数据分析工作SQL-JOIN的<span style="color: black;">运用</span>很频繁,下面这幅图<span style="color: black;">包括</span>了7种<span style="color: black;">状况</span>的JOIN语句用法。</span><span style="color: black;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/tOgI4VrBDEkoAG5ibZQSqiaGkLgvcjCJZKpXVaXcSMkoIGsTYH8InDA6mnHGUbaA1cpAdicTVic7cbVr8vwd2fXrAA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></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 style="color: black;">重点</span>分享INNER JOIN连接类型。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">01</strong></span></p><span style="color: black;"><strong style="color: blue;">语法结构</strong></span><span style="color: black;"><span style="color: black;">SELECT</span> <<span style="color: black;">select</span> <span style="color: black;">list</span>></span><span style="color: black;"><span style="color: black;">FROM</span> TableA A</span><span style="color: black;"><span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> TableB B</span><span style="color: black;"><span style="color: black;">ON</span> A. <span style="color: black;">Key</span> = B. <span style="color: black;">Key</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">A和B是表别名。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Key是两张表的<span style="color: black;">相关</span>字段,语法结构示例只有1个,<span style="color: black;">亦</span><span style="color: black;">能够</span>是多个,用and<span style="color: black;">就可</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">语法结构中的INNER关键字<span style="color: black;">能够</span>省略,即INNER JOIN 和 JOIN效果是<span style="color: black;">同样</span>的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INNER JOIN将返回左表和右表中的完全匹配的行。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">02</strong></span></p><span style="color: black;"><strong style="color: blue;">实例</strong></span><span style="color: black;"><span style="color: black;">这儿</span>利用<span style="color: black;">咱们</span>之前<span style="color: black;">文案</span>中创建的数据库</span><span style="color: black;">data_learning</span><span style="color: black;">,并在里面创建两张表,<span style="color: black;">插进</span>数据来演示</span><span style="color: black;">INNER JOIN</span><span style="color: black;"><span style="color: black;">怎样</span><span style="color: black;">运用</span>。</span><span style="color: black;"><span style="color: black;">-- 创建表table_a</span></span><span style="color: black;"><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> data_learning.table_a</span><span style="color: black;">(</span><span style="color: black;">p_name <span style="color: black;">VARCHAR</span>(<span style="color: black;">10</span>),</span><span style="color: black;">sales_volume <span style="color: black;">NUMERIC</span></span><span style="color: black;">);</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">插进</span>数据</span></span><span style="color: black;"><span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> data_learning.table_a</span><span style="color: black;"><span style="color: black;">VALUES</span>(<span style="color: black;">白雪公主</span>,<span style="color: black;">1000</span>),</span><span style="color: black;">(<span style="color: black;">卖火柴的小<span style="color: black;">女子</span></span>,<span style="color: black;">50</span>),</span><span style="color: black;">(<span style="color: black;">青蛙王子</span>,<span style="color: black;">500</span>);</span>
<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;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/tOgI4VrBDEkoAG5ibZQSqiaGkLgvcjCJZKI6bCavqNYYzEGDsacPPJCfH9lfQdcZEbVGYKWYD1Az4TN2kEpGe9lQ/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">-- 创建表 table_b</span></span><span style="color: black;"><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> data_learning.table_b</span><span style="color: black;">(</span><span style="color: black;">p_name <span style="color: black;">VARCHAR</span>(<span style="color: black;">10</span>),</span><span style="color: black;">price <span style="color: black;">NUMERIC</span></span><span style="color: black;">);</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">插进</span>数据</span></span><span style="color: black;"><span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> data_learning.table_b</span><span style="color: black;"><span style="color: black;">VALUES</span>(<span style="color: black;">白雪公主</span>,<span style="color: black;">16</span>),</span><span style="color: black;">(<span style="color: black;">卖火柴的小<span style="color: black;">女子</span></span>,<span style="color: black;">10</span>),</span><span style="color: black;">(<span style="color: black;">睡美人</span>,<span style="color: black;">13</span>);</span>
<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;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/tOgI4VrBDEkoAG5ibZQSqiaGkLgvcjCJZKAiawEIGTlXFQ6pRI5WtD0VOzg9WDCL9o4lICCiadf6NG2y2icY3Eic6GHA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上两张表的p_name差异在table_a表中有青蛙王子,而table_b表中<span style="color: black;">无</span>;table_b表中有睡美人,而table_a表中<span style="color: black;">无</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.1 INNER JOIN实例</span></p>
<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>以data_learning.table_a<span style="color: black;">做为</span>左表、data_learning.table_b<span style="color: black;">做为</span>右表进行INNER JOIN<span style="color: black;">相关</span>:</span></p><span style="color: black;"><span style="color: black;">SELECT</span></span><span style="color: black;"> a.*</span><span style="color: black;"> ,b.*</span><span style="color: black;"><span style="color: black;">FROM</span></span><span style="color: black;"> data_learning.table_a a</span><span style="color: black;"><span style="color: black;">INNER</span> <span style="color: black;">JOIN</span></span><span style="color: black;"> data_learning.table_b b</span><span style="color: black;"><span style="color: black;">ON</span> a.p_name = b. p_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">或省略INNER,语句如下:</span></p><span style="color: black;"><span style="color: black;">SELECT</span></span><span style="color: black;"> a.*</span><span style="color: black;"> ,b.*</span><span style="color: black;"><span style="color: black;">FROM</span></span><span style="color: black;"> data_learning.table_a a</span><span style="color: black;"><span style="color: black;">JOIN</span></span><span style="color: black;"> data_learning.table_b b</span><span style="color: black;"><span style="color: black;">ON</span> a.p_name = b. p_name</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 style="color: black;">查找</span>结果都是下图,<span style="color: black;">仅有</span>p_name完全匹配的数据才会被<span style="color: black;">查找</span>出来:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/tOgI4VrBDEkoAG5ibZQSqiaGkLgvcjCJZKpLdaocicgIOaRXdz39oCqKWwVQWibJKknW4J2tr5eU8ibUacRZUmonIog/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">03</strong></span></p><span style="color: black;"><strong style="color: blue;">小结</strong></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>
<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><span style="color: black;">SQL</span><span style="color: black;">多表连接的其中一种连接类型</span><span style="color: black;">INNER JOIN</span><span style="color: black;">的基本语法结构,<span style="color: black;">而后</span><span style="color: black;">运用</span></span><span style="color: black;">1</span><span style="color: black;">个简单实例<span style="color: black;">认识</span>了</span><span style="color: black;">INNER 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 style="color: black;"><span style="color: black;">小伙伴们<span style="color: black;">倘若</span>想要快速入门SQL<span style="color: black;">或</span>MySQL的话,我向<span style="color: black;">大众</span><span style="color: black;">举荐</span>一本广受欢迎的SQL经典畅销书籍——《SQL必知必会》。<span style="color: black;">或</span>关注我,查看后续更新的SQL教程系列哦~</span></span></span></p>
<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></p>
<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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 回顾历史,我们不难发现:无数先辈用鲜血和生命铺就了中华民族复兴的康庄大道。 真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。
页:
[1]