left join、 right join、union 与union all区别

左连接和有链接
通俗的讲:  
  A   left   join   B   的连接的记录数与A表的记录数同  
  A   right  join   B   的连接的记录数与B表的记录数同    
  A   left   join   B   等价B   right   join   A  


举个例子:  

  假设a表和b表的数据是这样的。  
  a                         b    
  id      name         id     stock   
  1      a             1         15  
  2        b             2         50  
  3        c                  
   
  select   *   from   a   inner   join   b   on   a.id=b.id  
  这个语法是连接查询中的内连接,它产生的结果是  
  两个表相匹配的记录出现在结果列表中。  
  根据上面的表,出现的结果是这样的  
  a.id     name         b.id     stock  
  1       a             1         15  
  2         b             2         50  
  ----------------------------  
  select   *   from   a,b   where   a.id=b.id  
  这个语法是内连接的另外一种写法,其执行结果与inner   join   一样  
   
  --------------------------------    
   
  select   *   from   a   left/right   join   b   on   a.id=b.id  
  这个是外连接语法中的左外连接或右外连接  
  如果是左外连接的话,它将显示a表的所有记录,  
  select   a.*,b.*   from   a   left   join   b   on   a.id=b.id  
  查询的结果是这样的:  
  a.id       name     b.id        stock  
  1         a         1          15  
  2           b         2          50  
  3           c       null         null   
  --------------------------------------------  
  如果是右外连接的话,它将显示b表的所有记录,  
  select   a.*,b.*   from   a   right   join   b   on   a.id=b.id  
  查询的结果是这样的:  
  a.id       name     b.id     stock  
  1         a         1             15  
  2           b         2             50   
****************************************************************************************8
union使用方法:


union 对两个结果集进行并集操作,重复数据只显示一次


Union All,对两个结果集进行并集操作,重复数据全部显示

example:
create table student

id int primary key,
name varchar(20),
age  number(20)
);
insert into student
(id ,name,age)
values
      ('1','student1','1'),
      ('2','student2','2'),
      ('3','student3','3'),
      ('4','student4','4'),
      ('5','student5','5'),
      ('6','student6','6'),
      ('7','student7','7');
 select * from student where id<5
 union
 select * from student where id>3 and id<8;
 显示:
 id         name         age
 1          student1     1
 2          student2     2
 3          student3     3
 4          student4     4


 select * from student where id<5
 union all
 select * from student where id>3 and id<8;
 显示:
 id         name         age
 1          student1     1
 2          student2     2
 3          student3     3
 4          student4     4
 4          student4     4
 5          student5     5
 6          student6     6
 7          student7     7


注意:
数据
4          student4     4
插入了两遍
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
left joinright join是SQL中的两种表连接方式。left join返回包括左表中的所有记录和右表中联结字段相等的记录,即左表的记录都会被返回,右表中没有匹配的记录会用NULL填充。right join则返回包括右表中的所有记录和左表中联结字段相等的记录,即右表的记录都会被返回,左表中没有匹配的记录会用NULL填充。 而union是用于合并两个结果集的操作,它会将两个结果集中的记录合并在一起,并且去除重复的记录。union all则是简单地将两个结果集合并在一起,不去除重复的记录。从效率上来说,union all比union快很多。所以,如果你确认合并的两个结果集中不包含重复数据且不需要排序,那么可以使用union all。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [多表联结(左连接(left join)、右连接(right join)、内连接(inner join)、unionunion all)](https://blog.csdn.net/qq_34801169/article/details/104917111)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [oracle知识整理(1) unionunion all的区别,left joinright join区别(各种join区别)](https://blog.csdn.net/moshowgame/article/details/103808897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值