leftjoin和leftouterjoin有什么区别
left join 是left outer join的简写,left join默认是outer属性的。
Inner Join 逻辑运算符返回满⾜第⼀个(顶端)输⼊与第⼆个(底端)输⼊联接的每⼀⾏。这个和⽤select查询多表是⼀样的效果,所以很少⽤到;
outer join则会返回每个满⾜第⼀个(顶端)输⼊与第⼆个(底端)输⼊的联接的⾏。它还返回任何在第⼆个输⼊中没有匹配⾏的第⼀个输⼊中的⾏。关键就是后⾯那句,返回的多⼀些。所以通常意义上的left join就是left outer join
在⼀个正规化的数据库环境中, 我们常会碰到这款情形: 所需的资料并不是放在同⼀个资料表中, 在这个时候, 你就要⽤到 Join。
当然 Join 如何将不同的数据库的资料结合, 还要看你如何使⽤它, ⼀共有四种不同的 Join 的⽅式, 在这篇⽂章中我们将为你介绍 Inner Join 及 Outer Join 以及其应⽤。
Inner Join
Inner Join 应该是最常⽤的 Join ⽅式, 它只会传回符合 Join 规则的纪录, 还是先来看看语法:
Select <;要选择的字段> From <;主要资料表> <Join ⽅式> <;次要资料表> [On <Join 规则>]
现在我们利⽤ MS SQL 内建的北风数据库来实际练习⼀下! 想多了解 MS SQL 的内建数据库, 你可以看看 SQL Server 的内建数据库这篇⽂章。
请打开 QA (Query Analyzer), 为了使⽤北风数据库, 请先执⾏ Use Northwind, 然后执⾏
Select ProductId, ProductName, SupplierId From Products
从 Products 产品资料表中取出三个字段, 分别是产品代码, 产品名称, 供货商代码, 但查询出来的结果保证让你的⽼板很不满意, 因为供货商代码对于⼈类实在是⽆什么意义, 这个时候 Join 就可以帮上忙了, 藉由 Join Suppliers 这个资料表我们便可以查询到供货商名称
Select ProductId, ProductName, Suppliers.SupplierId
From Products
Inner Join Suppliers on
Products.SupplierId= Suppliers.SupplierId
这款的查询结果是不是清楚呢! Inner Join 的主要精神就是 exclusive , 叫它做排他性吧! 就是讲 Join 规则不相符的资料就会被排除掉, 譬如讲在 Product 中有⼀项产品的供货商代码 (SupplierId), 没有出现在 Suppliers 资料表中, 那么这笔记录便会被排除掉
Outer Join
这款的 Join ⽅式是⼀般⼈⽐较少⽤到的, 甚⾄有些 SQL 的管理者也从未⽤过, 这真是⼀件悲哀的代志, 因为善⽤ Outer Join 是可以简化⼀些查询的⼯作的, 先来看看 Outer Join 的语法
Select <;要查询的字段> From <Left 资料表> <Left | Right> [Outer] Join <Right 资料表> On <Join 规则>
语法中的 Outer 是可以省略的, 例如你可以⽤ Left Join 或是 Right Join, 在本质上, Outer Join 是 inclusive, 叫它做包容性吧! 不同于 Inner Join 的排他性, 因此在 Left Outer Join 的查询结果会包含所有 Left 资料表的资料, 颠倒过来讲, Right Outer Join 的查询就会包含所有 Right 资料表的资料, 接下来我们还是来做些实际操作, 仍然是使⽤北风数据库, 但要先做⼀些⼩⼩的修改, 才能达到我们要的结果。
⾸先要拿掉 Products 资料表的 Foreign Key, 否则没有法度在 Products 资料表新增⼀笔 SupplierId 没有对映到 Suppliers 资料表的纪录,要知影⼀个资料表的 Constraint 你可以执⾏ SQL 内建的 sp_helpconstraint , 在 QA 执⾏
sp_helpconstraint Products
connect和join的区别接下来删除 FK_Products_Suppliers 这个 Foreign Key
Alter Table Products
Drop Constraint FK_Products_Suppliers
再来新增⼀笔纪录于 Products 资料表, SupplierId 使⽤ 50 是因为它并没有对映到 Suppliers 资料表中的记录
Insert Into Products (ProductName,SupplierId,CategoryId)
values ('Test Product','50','1')
现在我们再执⾏头前的查询, 只是将 Inner Join 改为 Left Outer Join
Select ProductId, ProductName, Suppliers.SupplierId
From Products
Left Outer Join Suppliers
Products.Suppliers = Suppliers.SupplierId
⽐较⼀下两种 Join ⽅式的查询结果, 你应该就会知影其中的差别!
再来看看 Right Outer Join, 请新增下底这笔记录
Insert Into Suppliers (CompanyName)
values ('LearnASP')
现在请使⽤ Right Out Join 来作查询, ⽐较看看查询的结果和 Inner Join 有什么不同!
寻不相符纪录
这⾥我们来看看如何使⽤ Out Join 来不相符纪录, 可能是有⼦纪录却没有⽗纪录或是颠倒过来
Select Suppliers.CompanyName From Products
Right Join Suppliers
On Products.SupplierId = Suppliers.SupplierId
Where Products.SupplierId is Null
执⾏结果你会到⼀笔资料为 LearnASP, 该笔供货商资料存在, 但基本上已经没有产品是来⾃这个供货商, 想象⼀下如果不⽤ Outer Join 你要怎么以⼀个 SQL 指令完成同⼀查询结果! 知道 Outer Join 的好⽤了吧! 再执⾏
Select Products.ProductName
From Products
Left Join Suppliers
On Products.SupplierId = Suppliers.SupplierId
Where Suppliers.SupplierId is Null
这个查询结果你会发现 Test Product 这项产品竟然不到供货商的资料!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。