图解SQL的各种连接(Inner join,outer join,left join,right join)

由于 SQL Join 似乎被默认为基础,同时利用 ‘文氏图表’ 解释它,乍一看似乎是很自然的选择。然而,就像文章下面说的,我也发现在实际测试中,文氏图并没有完全符合SQL Join 语法。


     通过图文并茂的方式对SQL的Join进行简单的介绍:join大致分为以下七种情况:


1准备数据

  1. DROP TABLE [dbo].[test_a]  
  2. GO  
  3. CREATE TABLE [dbo].[test_a] (  
  4. [id] int NULL ,  
  5. [name] varchar(255) NULL   
  6. )  
  7. GO  
  8. -- ----------------------------  
  9. -- Records of test_a  
  10. -- ----------------------------  
  11. INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'1', N'苹果')  
  12. GO  
  13. GO  
  14. INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'2', N'橘子')  
  15. GO  
  16. GO  
  17. INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'3', N'菠萝')  
  18. GO  
  19. GO  
  20. INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'4', N'香蕉')  
  21. GO  
  22. GO  
  23. INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'5', N'西瓜')  
  24. GO  
  25. GO  
  26. -----------------------------------------------------------  
  27. DROP TABLE [dbo].[test_b]  
  28. GO  
  29. CREATE TABLE [dbo].[test_b] (  
  30. [id] int NULL ,  
  31. [name] varchar(255) NULL   
  32. )  
  33. GO  
  34.   
  35. -- ----------------------------  
  36. -- Records of test_b  
  37. -- ----------------------------  
  38. INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'1', N'梨子')  
  39. GO  
  40. GO  
  41. INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'2', N'苹果')  
  42. GO  
  43. GO  
  44. INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'3', N'草莓')  
  45. GO  
  46. GO  
  47. INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'4', N'桃子')  
  48. GO  
  49. GO  
  50. INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'5', N'香蕉')  
  51. GO  
  52. GO   

2示例介绍

A.Inner join

产生A和B的交集。

  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. INNER JOIN test_b ON test_a.name = test_b.name   
222

B. Full outer join

产生A和B的并集。对于没有匹配的记录,则以null做为值。
  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. FULL OUTER JOIN test_b ON test_a.name = test_b.name   
222

C.Left outer join 

产生表A的完全集,而B表中匹配的则有值,没匹配的以null值取代。
  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. LEFT OUTER JOIN test_b ON test_a.name = test_b.name   
222

D. Left outer join on where

产生在A表中有而在B表中没有的集合。

  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. LEFT OUTER JOIN test_b ON test_a.name = test_b.name  
  6. WHERE  
  7.     test_b.name IS NULL   
222

E. RIGHT OUTER JOIN

产生表B的完全集,而A表中匹配的则有值,没匹配的以null值取代。
  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. RIGHT OUTER JOIN test_b ON test_a.name = test_b.name   
222

F. right outer join on where

产生在B表中有而在A表中没有的集合。
  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. RIGHT OUTER JOIN test_b ON test_a.name = test_b.name  
  6. WHERE  
  7.     test_a.name IS NULL   
222

G. FULL OUTER JOIN WHERE

产生(A表中有但B表没有)和(B表中有但A表中没有)的数据集。
  1. SELECT  
  2.     *  
  3. FROM  
  4.     test_a  
  5. FULL OUTER JOIN test_b ON test_a.name = test_b.name  
  6. WHERE  
  7.     test_a.name IS NULL  
  8. OR test_b.name IS NULL   
222

H. cross join

表A和表B的数据进行一个N*M的组合,即笛卡尔积(交差集)。一般来说,我们很少用到这个语法。因为这种集合的使用对于性能来说非常危险,尤其是表很大。这里就不做介绍了…
                                                            内容更新于:2017-02-17
注释: 在某些数据库中, RIGHT JOIN 称为 RIGHT OUTER JOIN。LEFT JOIN 称为 LEFT OUTER JOIN。
转载于:https://blog.csdn.net/zhushuhai331/article/details/50477390
                              

猜你喜欢

转载自blog.csdn.net/yinni11/article/details/80172982