sql中的上下相关、全相关、自相关
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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/0b64b85b1b4146298ec1c83bc423f7c0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346624&x-signature=uYzGRrDzhaeRKxFkMPyr%2Bqjq33w%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">left join(左联接) 返回<span style="color: black;">包含</span>左表中的所有记录和右表中联结字段相等的记录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">right join(右联接) 返回<span style="color: black;">包含</span>右表中的所有记录和左表中联结字段相等的记录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">inner join(等值连接) 只返回两个表中联结字段相等的行</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;">--------------------------------------------</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表A记录如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">aID aNum</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 a20050111</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 a20050112</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 a20050113</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 a20050114</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5 a20050115</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表B记录如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bID bName</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 2006032401</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 2006032402</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 2006032403</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 2006032404</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">8 2006032408</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;">1.left join</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sql语句如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select * from A</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">left join B</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">on A.aID = B.bID</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;">aID aNum bID bName</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 a20050111 1 2006032401</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 a20050112 2 2006032402</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 a20050113 3 2006032403</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 a20050114 4 2006032404</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5 a20050115 NULL NULL</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(所影响的行数为 5 行)</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;">left join是以A表的记录为<span style="color: black;">基本</span>的,A<span style="color: black;">能够</span>看成左表,B<span style="color: black;">能够</span>看成右表,left join是以左表为准的.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">换句话说,左表(A)的记录将会<span style="color: black;">所有</span><span style="color: black;">暗示</span>出来,而右表(B)只会<span style="color: black;">表示</span>符合搜索<span style="color: black;">要求</span>的记录(例子中为: A.aID = B.bID).</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">B表记录不足的<span style="color: black;">地区</span>均为NULL.</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;">2.right join</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sql语句如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select * from A</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">right join B</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">on A.aID = B.bID</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;">aID aNum bID bName</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 a20050111 1 2006032401</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 a20050112 2 2006032402</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 a20050113 3 2006032403</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 a20050114 4 2006032404</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">NULL NULL 8 2006032408</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(所影响的行数为 5 行)</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>,和left join的结果刚好相反,这次是以右表(B)为<span style="color: black;">基本</span>的,A表不足的<span style="color: black;">地区</span>用NULL填充.</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;">3.inner join</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sql语句如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select * from A</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">innerjoin B</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">on A.aID = B.bID</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;">aID aNum bID bName</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 a20050111 1 2006032401</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 a20050112 2 2006032402</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 a20050113 3 2006032403</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 a20050114 4 2006032404</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;">这儿</span>只<span style="color: black;">表示</span>出了 A.aID = B.bID的记录.这说明inner 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;">--------------------------------------------</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;">LEFT JOIN操<span style="color: black;">功效</span>于在任何的 FROM 子句中,组合<span style="color: black;">源自</span>表的记录。使用 LEFT JOIN 运算来创建一个左边<span style="color: black;">外边</span>联接。左边<span style="color: black;">外边</span>联接将<span style="color: black;">包括</span>了从<span style="color: black;">第1</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>可在FROM或WHERE子句中指定,<span style="color: black;">意见</span>在FROM子句中指定连接<span style="color: black;">要求</span>。WHERE和HAVING子句<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;">连接可分为以下几类:</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>
<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>表共有的列的值匹配两个表中的行。例如,检索 students 和 courses 表中学生标识号相同的所有行。</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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在FROM子句中指定外连接时,<span style="color: black;">能够</span>由下列几组关键字中的一组指定:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LEFT JOIN 或 LEFT OUTER JOIN。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左向外连接的结果集<span style="color: black;">包含</span>LEFT OUTER子句中指定的左表的所有行,而<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;">RIGHT JOIN 或 RIGHT OUTER JOIN。</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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FULL JOIN 或 FULL OUTER JOIN。</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>
<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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">USE pubs</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT a.au_fname, a.au_lname, p.pub_name</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM authors AS a INNER JOIN publishers AS p</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ON a.city = p.city</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AND a.state = p.state</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ORDER BY a.au_lname ASC, a.au_fname ASC</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM 子句中的表或视图可<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 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;">a表 id name b表 id job parent_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 张3 1 23 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 李四 2 34 2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 王武 3 34 4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.id同parent_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;">select a.*,b.* from a inner join b on a.id=b.parent_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;">1 张3 1 23 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 李四 2 34 2</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;">select a.*,b.* from a left join b on a.id=b.parent_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;">1 张3 1 23 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 李四 2 34 2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 王武 null</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;">select a.*,b.* from a right join b on a.id=b.parent_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;">1 张3 1 23 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 李四 2 34 2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">null 3 34 4</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;">select a.*,b.* from a full join b on a.id=b.parent_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;">1 张3 1 23 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 李四 2 34 2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">null 3 34 4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 王武 null</p>
你的见解独到,让我受益匪浅,期待更多交流。 谢谢、感谢、感恩、辛苦了、有你真好等。 这夸赞甜到心里,让我感觉温暖无比。
页:
[1]