`
jaychang
  • 浏览: 716180 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

内连接与外连接

阅读更多

最近一段时间找工作,对以往的知识进行了复习,以下是本人对数据库中表连接的总结:

 

连接:连接是指将关系数据库中的两个表根据内容一定的条件连接成一个表

 

连接类型:内连接与外连接

 

a、内连接:又称等值连接,如

 

Java代码
  1. Select a.*,b.* From TableA a,TableB b where a.id = b.id;  
Select a.*,b.* From TableA a,TableB b where a.id = b.id;

 

 也可以:

 

Java代码
  1. Select a.*,b.* From TableA a INNER JOIN TableB b ON a.id = b.id;  
Select a.*,b.* From TableA a INNER JOIN TableB b ON a.id = b.id;

 

 

b、外连接(左外连接、右外连接、迫切左连接)

 

左外连接:以左表为基础来连接,如果左表的某行内容无法在右表中找到相对的row,则将右表统统用null来表示

 

Java代码
  1. Select a.*,b.* From TableA a LEFT JOIN TableB b ON a.id = b.id;  
Select a.*,b.* From TableA a LEFT JOIN TableB b ON a.id = b.id;

 

 

右外连接:以右表为基础来连接,如果右表的某行内容无法在左表中找到相对的row,则将左表统统用null来表示

 

Java代码
  1. Select a.*,b.* From TableA a RIGHT JOIN TableB b ON a.id = b.id;  
Select a.*,b.* From TableA a RIGHT JOIN TableB b ON a.id = b.id;

 

 

迫切左连接:解决N+1问题

如:A表与B表相连,B表与C表相连。当A与B表左连接( LEFT JOIN时,在加载B的同
时,也会加载与B相关联的C表;

                                                  当A与B表迫切左连接时(LEFT JOIN FETCH),只保证加载需要的实体,即B

 

Java代码
  1. Select a.*,b.* From TableA a LEFT JOIN FETCH TableB b   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics