图示数据库表连接,秒懂左连接,右连接,内连接...
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Java识堂</strong>,一个高原创,高<span style="color: black;">保藏</span>,有干货的<strong style="color: blue;"><span style="color: black;">微X</span>公众号,</strong><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>建模(model)。</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>"模型"<span style="color: black;">便是</span>两样东西:实体(entity)+ 关系(relationship)。</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>实体之间的联系,<span style="color: black;">一般</span><span style="color: black;">能够</span>分成"一对一"、"一对多"和"多对多"等类型。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/aea6134b59a7476999755c56be58799f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=PaQ2cE3R3Sl6hMQf1bVQK3dnGto%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在关系型数据库里面,<span style="color: black;">每一个</span>实体有自己的一张表(table),所有属性都是这张表的字段(field),表与表之间<span style="color: black;">按照</span><span style="color: black;">相关</span>字段"连接"(join)在<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>内连接(inner join)外连接(outer join)左连接(left join)右连接(right join)全连接(full join)<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>连接的差异。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/df593eaaf7df466c837f243ed091c078~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=yTH8IP5GzPucbRq%2BM3%2FBbLyUDY0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/28f2b0d82b8e43c8a6225a96b6bdcc85~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=psSoNd5T75CZeZq5h66quoVItA4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/3a9174e1055e411891640736058b9a36~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=Ye50lkj7VDHoP5ho7Rb8mXVMEME%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c95a799e42c44795b99e1551fc95808a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=Dn75aQIk76dOBX8yK2pK0%2B7fFSA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<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;"><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>,表 A <span style="color: black;">包括</span>张三和李四,表 B <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>。</p>只返回两张表匹配的记录,这叫内连接(inner join)。返回匹配的记录,以及表 A 多余的记录,这叫左连接(left join)。返回匹配的记录,以及表 B 多余的记录,这叫右连接(right join)。返回匹配的记录,以及表 A 和表 B 各自的多余记录,这叫全连接(full join)。<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>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/d173ace5479b4dbe8662f60bdd550041~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=tiE1E4kXMRexCWX7TyoggETpg3U%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上图中,表 A 的记录是 123,表 B 的记录是 ABC,颜色<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>分成两大类:内连接(inner join)<span style="color: black;">暗示</span>只<span style="color: black;">包括</span>匹配的记录,外连接(outer join)<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;">这四种连接的 SQL 语句如下。</p>SELECT * FROM A
INNER JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
LEFT JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
RIGHT JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
FULL JOIN B ON A.book_id=B.book_id;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面的 SQL 语句还<span style="color: black;">能够</span>加上where<span style="color: black;">要求</span>从句,对记录进行筛选,<span style="color: black;">例如</span>只返回表 A 里面不匹配表 B 的记录。</p>SELECT * FROM A
LEFT JOIN B
ON A.book_id=B.book_id
WHERE B.id IS null;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一个例子,返回表 A 或表 B 所有不匹配的记录。</p>SELECT * FROM A
FULL JOIN B
ON A.book_id=B.book_id
WHERE A.id IS null OR B.id IS null;
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另外</span>,还存在一种特殊的连接,叫做"交叉连接"(cross join),指的是表 A 和表 B 不存在<span style="color: black;">相关</span>字段,<span style="color: black;">此时</span>表 A(共有 n 条记录)与表 B (共有 m 条记录)连接后,会产生一张<span style="color: black;">包括</span> n x m 条记录的新表(见下图)。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/6974ccb30800427586d0395f8d398853~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339551&x-signature=9rQAjP%2Fe8jcBzdMHB3YjgHMf2xY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<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;">链接:</strong></p><strong style="color: blue;">http://www.ruanyifeng.com/blog/2019/01/table-join.html</strong>
</div>
你的见解真是独到,让我受益匪浅。 谷歌外贸网站优化技术。 同意、说得对、没错、我也是这么想的等。 谷歌外链发布 http://www.fok120.com/ 你的话语如春风拂面,温暖了我的心房,真的很感谢。 你的见解独到,让我受益匪浅,非常感谢。 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/
页:
[1]