【SQL 學習】表連線

楊奇龍發表於2010-08-15

朋友問了關於 表連線的問題,在這裡用實驗說明一下:
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 為空的列

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-671023/,如需轉載,請註明出處,否則將追究法律責任。

相關文章