【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 为空的列

上一篇:Linux Study之--通过udev创建ASM DISK(RAC共享存储)


下一篇:C# 根据BackgroundWorker异步模型和ProgressBar控件,自定义进度条控件