【SQL 学习】表连接-阿里云开发者社区

开发者社区> 北在南方> 正文

【SQL 学习】表连接

简介:
+关注继续查看

朋友问了关于 表连接的问题,在这里用实验说明一下:
SQL> select
  2  product_id,
  3  product_type_id,
  4  name
  5  from products;

PRODUCT_ID PRODUCT_TYPE_ID NAME                                                
---------- --------------- ------------------------------                  
         1               1 Modern
         2               1 Chemistry                                       
         3               2 Supernova                                       
         4               2 Tank War                                        
         5               2 Z Files                                         
         6               2 2412: The Return                                
         7               3 Space Force 9                                   
         8               3 From Another Planet                             
         9               4 Classical Music                                 
        10               4 Pop 3                                           
        11               4 Creative Yell                                   
        12                 My Front Line-my front line的product_type_id为空                                  
已选择12行。

SQL> desc product_types
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 PRODUCT_TYPE_ID                           NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(10)

SQL> select * from product_types;

PRODUCT_TYPE_ID NAME                                                           
--------------- ----------                                                     
              1 Book                                                           
              2 Video                                                          
              3 DVD                                                            
              4 CD                                                             
              5 Magazine 
---上面两个查询是例子中用到的表                                                     
---内连接:只要当连接中的列包含满足连接条件的值时才会返回一行.就是说,如果某一行的连接条件中的一列值为空,那么这行就不返回.
对比 下面的内连接和外连接 发现内连接返回11行,而外连接返回12行,因为外连接可以返回值为空的行.

SQL> select p.name,pt.name
  2  from products p,product_types pt
  3  where p.product_type_id=pt.product_type_id --内连接
  4  order by p.name;
NAME                           NAME                                            
------------------------------ ----------                                      
2412: The Return               Video                                           
Chemistry                      Book                                            
Classical Music                CD                                              
Creative Yell                  CD                                              
From Another Planet            DVD                                             
Modern Science                 Book                                            
Pop 3                          CD                                              
Space Force 9                  DVD                                             
Supernova                      Video                                           
Tank War                       Video                                           
Z Files                        Video                                           
已选择11行。

SQL>--外连接:即使连接条件中的一列包含空值也会返回一行,如 My Front Line对应的product_types.name
SQL>--和Magazine对应的product.name 列
SQL> select p.name,pt.name
  2  from products p full outer join product_types pt
  3  using (product_type_id)
  4  order by p.name;
NAME                           NAME                                            
------------------------------ ----------                                      
2412: The Return               Video                                           
Chemistry                      Book                                            
Classical Music                CD                                              
Creative Yell                  CD                                              
From Another Planet            DVD                                             
Modern Science                 Book                                            
My Front Line                  --为空                                               
Pop 3                          CD                                              
Space Force 9                  DVD                                             
Supernova                      Video                                           
Tank War                       Video                                           
Z Files                        Video                                           
--为空                         Magazine                                        
已选择13行。
SQL> --左外连接:当等号右边的列对应为空时,仍然显示.
SQL> select p.name,pt.name
  2  from products p,product_types pt
  3  where p.product_type_id=pt.product_type_id(+)
  4  order by p.name;

NAME                           NAME                                            
------------------------------ ----------                                      
2412: The Return               Video                                           
Chemistry                      Book                                            
Classical Music                CD                                              
Creative Yell                  CD                                              
From Another Planet            DVD                                             
Modern Science                 Book                                            
My Front Line                  ---为空                                                
Pop 3                          CD                                              
Space Force 9                  DVD                                             
Supernova                      Video                                           
Tank War                       Video                                           
Z Files                        Video                                           
已选择12行。

SQL> ---My Front Line 对应的类型为空 左外连接显示product_types表中name为空的列
SQL> --右外连接:当等号左边的列对应为空时,仍然显示.

SQL> select p.name,pt.name
  2  from products p,product_types pt
  3  where p.product_type_id(+)=pt.product_type_id
  4  order by p.name;

NAME                           NAME                                            
------------------------------ ----------                                      
2412: The Return               Video                                           
Chemistry                      Book                                            
Classical Music                CD                                              
Creative Yell                  CD                                              
From Another Planet            DVD                                             
Modern Science                 Book                                            
Pop 3                          CD                                              
Space Force 9                  DVD                                             
Supernova                      Video                                           
Tank War                       Video                                           
Z Files                        Video                                           
 --为空                        Magazine  --my front line的product_type_id为空                                      

已选择12行。
SQL> -- name 列有值为空,右外连接显示product 表中 name 为空的列

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
一文快速搞懂系列__一文快速搞懂SuperSet[实战案例]
大家好,我是**ChinaManor**,直译过来就是中国码农的意思,俺希望自己能成为国家复兴道路的铺路人,大数据领域的耕耘者,平凡但不甘于平庸的人。
9 0
阿里巴巴数据库分库分表的实践(5)
阿里巴巴数据库分库分表的实践(5)
10 0
一文快速了解ClickHouse 战斗民族的开源搜索引擎(超详细解读+快速入门)
大家好,我是**ChinaManor**,直译过来就是中国码农的意思,俺希望自己能成为国家复兴道路的铺路人,大数据领域的耕耘者,一个平凡而不平庸的人。
8 0
PG+MySQL第9课-实时精准营销
通常业务场景会涉及基于标签条件圈选目标客户、基于用户特征值扩选相似人群、群体用户画像分析这些技术,本文将围绕这三个场景去介绍在实施精准营销里面的PG数据库的使用
6 0
阿里巴巴数据库分库分表的实践(6)
阿里巴巴数据库分库分表的实践(6)
9 0
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(6)
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(6)
8 0
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(4)
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(4)
5 0
阿里巴巴数据库分库分表的实践(4)
阿里巴巴数据库分库分表的实践(4)
7 0
MySQL高可用架构演进
MySQL是数据库领域当之无愧的霸主之一,其在各行各业被广泛应用,随着广泛使用,对于MySQL本身的高可用性的要求就是不可避免的话题,而MySQL的高可用方案也随着MySQL功能的完善经历了多次升级,本文将对MySQL的各种高可用架构进行分析,以此来了解架构的演进。
6 0
阿里巴巴数据库分库分表的实践(3)
阿里巴巴数据库分库分表的实践(3)
5 0
+关注
640
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载