数据库连接总结(内连接,外连接,全连接)
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">平常</span>连接类型</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/435a4e7989384086a6c76146f9eb9900~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=3Ioo1vevazYkmpJEKoWs51x1MkI%3D" 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;">归类</span></p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文中<span style="color: black;">运用</span>数据库环境:MySQL 5.7</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">SQL joins 经典图</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/7afad12dba5f439cb5743f6423e526be~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=BKKnKwa5Ovy0gixJsIfqehuf1dw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">准备SQL语句</h1><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> <span style="color: black;">`t_emp`</span> (
<span style="color: black;">`id`</span> <span style="color: black;">int</span>(<span style="color: black;">11</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> AUTO_INCREMENT,
<span style="color: black;">`name`</span> <span style="color: black;">varchar</span>(<span style="color: black;">20</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
<span style="color: black;">`deptId`</span> <span style="color: black;">int</span>(<span style="color: black;">11</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
PRIMARY <span style="color: black;">KEY</span> (<span style="color: black;">`id`</span>) ,
<span style="color: black;">KEY</span> <span style="color: black;">`fk_dept_id`</span>(<span style="color: black;">`deptId`</span>)
)<span style="color: black;">ENGINE</span> = <span style="color: black;">InnoDB</span> AUTO_INCREMENT = <span style="color: black;">1</span> <span style="color: black;">CHARACTER</span> <span style="color: black;">SET</span> = utf8;
<span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> <span style="color: black;">`t_dept`</span> (
<span style="color: black;">`id`</span> <span style="color: black;">int</span>(<span style="color: black;">11</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span>AUTO_INCREMENT,<span style="color: black;">`deptName`</span> <span style="color: black;">varchar</span>(<span style="color: black;">30</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
<span style="color: black;">`locAdd`</span> <span style="color: black;">varchar</span>(<span style="color: black;">40</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
PRIMARY <span style="color: black;">KEY</span> (<span style="color: black;">`id`</span>)
) <span style="color: black;">ENGINE</span> = <span style="color: black;">InnoDB</span> AUTO_INCREMENT = <span style="color: black;">1</span> <span style="color: black;">CHARACTER</span> <span style="color: black;">SET</span>= utf8;<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_dept(deptName,locAdd) <span style="color: black;">VALUES</span>(<span style="color: black;"><span style="color: black;">开发</span>部</span>, <span style="color: black;">21</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_dept(deptName,locAdd) <span style="color: black;">VALUES</span>(<span style="color: black;">人力资源部</span>, <span style="color: black;">22</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_dept(deptName,locAdd) <span style="color: black;">VALUES</span>(<span style="color: black;">市场部</span>, <span style="color: black;">23</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_dept(deptName,locAdd) <span style="color: black;">VALUES</span>(<span style="color: black;">后勤部</span>, <span style="color: black;">24</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_dept(deptName,locAdd) <span style="color: black;">VALUES</span>(<span style="color: black;">总经办</span>, <span style="color: black;">25</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">张三</span>, <span style="color: black;">1</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span>t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">李四</span>, <span style="color: black;">1</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">王五</span>, <span style="color: black;">1</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">赵六</span>, <span style="color: black;">2</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">刘一</span>, <span style="color: black;">2</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">钱七</span>, <span style="color: black;">3</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">关八</span>, <span style="color: black;">4</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t_emp(<span style="color: black;">NAME</span>, deptId) <span style="color: black;">VALUES</span>(<span style="color: black;">孙九</span>, <span style="color: black;">51</span>);
<h1 style="color: black; text-align: left; margin-bottom: 10px;"> 交叉连接(cross join)</h1>
<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;">实现方式:</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;">(1)隐式交叉连接(<span style="color: black;">无</span>CROSS JOIN)</span></strong></p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td ,t_emp te ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(2)</span><strong style="color: blue;"><span style="color: black;">显式交叉连接</span></strong></p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">cross</span> <span style="color: black;">join</span> t_emp te ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/7b832c215264416da2df899159090754~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=pmx1EDQ%2Frdv6ic%2BzkYq2zl%2Bw1cg%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">笛卡尔积结果</p>
</div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">内连接 (inner join)</h1>
<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 style="color: black;">方向</span>讲<span style="color: black;">便是</span>从笛卡尔积中挑出ON</span><strong style="color: blue;"><span style="color: black;">子句<span style="color: black;">要求</span>成立</span></strong><span style="color: black;">的记录。 <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 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></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 style="color: black;">select</span></span></strong><span style="color: black;"><span style="color: black;"> * </span></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">from</span></span></strong><span style="color: black;"><span style="color: black;"> t_dept td </span></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">inner</span></span></strong><strong style="color: blue;"><span style="color: black;"><span style="color: black;">join</span></span></strong><span style="color: black;"><span style="color: black;"> t_emp te </span></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">on</span></span></strong><span style="color: black;"><span style="color: black;"> td .id > te.id </span></span><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;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p9-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/65f9d5c49452496a9ff1379b03b612c2~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=Yh61YmsiTPNexsCQuy0L%2BnLm%2BTg%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>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1.1.1.6. 等值连接 (equijoin)</h1>
<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>θ连接)在连接运算符为“=”号时,即θ=0时的一个特例;等值连接是一种特殊内连接。</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;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">inner</span> <span style="color: black;">join</span> t_emp te <span style="color: black;">on</span> td .id = te.deptId ;
<span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td, t_emp te <span style="color: black;">where</span> td .id = te.deptId ;
<span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">join</span>t_emp te<span style="color: black;">on</span> td .id = te.deptId ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/95796f1e0933485dac815bb734ddd799~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=amxWN38%2Fgwj2Bala1dVsaDDmY%2BY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"> 自然连接(natural join)</h1>
<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;">实现方式:</p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">natural</span> <span style="color: black;">join</span>t_emp te<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d244231336d841738ec4681f7d93ac75~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=QbnxLJ4kNXUOe3dOhRCN8h0YzVo%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">左连接(left join)</h1>
<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>先从笛卡尔积中挑出ON子句<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/tos-cn-i-qvj2lq49k0/328bbe6f40724b86b7b55c80b5cd4e41~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=CY7VIPxchoH8mEQqwLda%2Bc786PQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现方式:</p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">left</span> <span style="color: black;">join</span> t_emp te <span style="color: black;">on</span>td .id = te.deptId ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/91ad0617ab8943aea5bd384da915766b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=em0creS9oZOuwI32tn6bekIn8aE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">右连接</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">右连接RIGHT JOIN<span style="color: black;">便是</span>求两个表左和右表的交集外加右表B剩下的数据。再次从笛卡尔积的<span style="color: black;">方向</span>描述,右连接<span style="color: black;">便是</span>从笛卡尔积中挑出ON子句<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/tos-cn-i-qvj2lq49k0/3ac8c9f2e7444f18bcc5ea598ba7ca96~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=y0fzgorKjbz%2BzYGOwiDqS1PRM8E%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现方式:</p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">right</span> <span style="color: black;">join</span> t_emp te <span style="color: black;">on</span>td .id = te.deptId ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/c82f6fb58c20433f8ff344c1eaa8dc1c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=9JjGwXEeJ6UenTB6MFR2ay%2BMpFM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"> 全连接</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">全外连接<span style="color: black;">便是</span>求两个表A和B集合的并集。从笛卡尔积的<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>MySQL不支持OUTER JOIN,<span style="color: black;">然则</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>对左连接和右连接的结果做 UNION 操作来实现。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/35ce663bd3064581a36a64bb7fcd378c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=NanVv%2BLRO3f06J9LJvqrxOUnDdU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现方式:</p><span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">left</span> <span style="color: black;">join</span> t_emp te <span style="color: black;">on</span> td .id = te.deptId
<span style="color: black;">union</span>
<span style="color: black;">select</span> * <span style="color: black;">from</span> t_dept td <span style="color: black;">right</span> <span style="color: black;">join</span> t_emp te <span style="color: black;">on</span> td .id = te.deptId ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/10214cd4b9de4e2ea54c4128e4981f5c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338451&x-signature=vzHro5XTFWB2gEdMsX6R%2F2JvqOw%3D" style="width: 50%; margin-bottom: 20px;"></div>
谷歌外贸网站优化技术。 你的话语真是温暖如春,让我心生感激。 同意、说得对、没错、我也是这么想的等。 外贸B2B平台有哪些? 你的见解独到,让我受益匪浅,非常感谢。
页:
[1]