SQL SERVER:多表查询之—内连接(20)
<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;">1、多表<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;">2、多表<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;">1)连接<span style="color: black;">查找</span>:内连接、左外连接、右外连接、自连接;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)合并<span style="color: black;">查找</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)子<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;">2、</span>内连接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、内连接的特点</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;">B、参与<span style="color: black;">查找</span>的两个表的地位相同,无主次之分;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C、执行<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;">2、内连接<span style="color: black;">查找</span>语法</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;">SELECT <字段列表 > FROM <表1,表2 ...> WHERE <span style="color: black;">要求</span>....</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;">SELECT <字段列表 > FROM <表1> JOIN <表2> ON <连接条件> ...</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、</span>例:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/e69c13a6a3584e03a5dccfb57849f4c0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723340412&x-signature=qWP%2FOlKCWv9%2BatRPK6bZ9E5oygY%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">数据库MyServer中的表关系示意图</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、<span style="color: black;">查找</span>MyServer数据库中<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;">A、隐式内连接</p><span style="color: black;">USE</span> MyServer
<span style="color: black;">GO</span>
<span style="color: black;">SELECT</span> E_Name,E_Gender,E_Age, D_Name <span style="color: black;">FROM</span> Employee,Department
<span style="color: black;">WHERE</span> Employee.D_Id=Department.D_Id
<span style="color: black;">GO</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">B:显式内连接</p><span style="color: black;">USE</span> MyServer
<span style="color: black;">GO</span>
<span style="color: black;">SELECT</span> E_Name,E_Gender,E_Age, D_Name
<span style="color: black;">FROM</span> Employee <span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> Department
<span style="color: black;">ON</span> Employee.D_Id=Department.D_Id
<span style="color: black;">GO</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、<span style="color: black;">查找</span>MyServer数据库中属于“仓储部”的人员记录,<span style="color: black;">包括</span>姓名、性别、年龄、<span style="color: black;">分部</span>名<span style="color: black;">叫作</span></p><span style="color: black;">USE</span> MyServer
<span style="color: black;">GO</span>
<span style="color: black;">SELECT</span> E_Name,E_Gender,E_Age, D_Name
<span style="color: black;">FROM</span> Employee <span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> Department
<span style="color: black;">ON</span>Employee.D_Id=Department.D_Id<span style="color: black;">WHERE</span> Department.D_Id=<span style="color: black;">3</span>
<span style="color: black;">GO</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、在MyServer数据库中<span style="color: black;">查找</span>姓名为麻六的员工,所<span style="color: black;">营销</span><span style="color: black;">制品</span>名<span style="color: black;">叫作</span>为“Excel函数”的销量</p><span style="color: black;">USE</span> MyServer
<span style="color: black;">GO</span>
<span style="color: black;">SELECT</span> E.E_Name,P.PL_Name,S.S_Amount <span style="color: black;">FROM</span> Employee E
<span style="color: black;">JOIN</span>SalesList S<span style="color: black;">ON</span> E.E_Id=S.E_Id
<span style="color: black;">JOIN</span> ProductList P <span style="color: black;">ON</span> P.PL_Id=S.PL_ID
<span style="color: black;">WHERE</span> E.E_Name=<span style="color: black;">麻六</span> <span style="color: black;">AND</span> P.PL_Name=<span style="color: black;">Excel函数</span>
<span style="color: black;">GO</span>
真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。 系统提示我验证码错误1500次 \~゛, 感谢楼主的分享!我学到了很多。
页:
[1]