Oracle HR模式在员工表上自行加入

时间:2015-03-07 11:12:42

标签: sql oracle oracle11g self-join

我在oracles HR模式上执行了以下正确的自联接查询,但我无法真正理解它返回的内容。当我执行完全相同的查询但使用LEFT JOIN时我理解它返回所有员工,无论他们是否有主管。

经理ID有点令人困惑,例如156, King - 但King的ID为100.

SELECT emps.employee_id as "Employee", emps.last_name, mgr.employee_id as "Manager", mgr.last_name
FROM employees emps
RIGHT JOIN employees mgr
ON emps.manager_id = mgr.employee_id

结果

Employee LAST_NAME                    Manager LAST_NAME              
---------- ------------------------- ---------- -------------------------
   101 Kochhar                          100 King                      
   102 De Haan                          100 King                      
   103 Hunold                           102 De Haan                   
   104 Ernst                            103 Hunold                    
   105 Austin                           103 Hunold                    
   106 Pataballa                        103 Hunold                    
   107 Lorentz                          103 Hunold                    
   108 Greenberg                        101 Kochhar                   
   109 Faviet                           108 Greenberg                 
   110 Chen                             108 Greenberg                 
   111 Sciarra                          108 Greenberg                 
   112 Urman                            108 Greenberg                 
   113 Popp                             108 Greenberg                 
   114 Raphaely                         100 King                      
   115 Khoo                             114 Raphaely                  
   116 Baida                            114 Raphaely                  
   117 Tobias                           114 Raphaely                  
   118 Himuro                           114 Raphaely                  
   119 Colmenares                       114 Raphaely                  
   120 Weiss                            100 King                      
   121 Fripp                            100 King                      
   122 Kaufling                         100 King                      
   123 Vollman                          100 King                      
   124 Mourgos                          100 King                      
   125 Nayer                            120 Weiss                     
   126 Mikkilineni                      120 Weiss                     
   127 Landry                           120 Weiss                     
   128 Markle                           120 Weiss                     
   129 Bissot                           121 Fripp                     
   130 Atkinson                         121 Fripp                     
   131 Marlow                           121 Fripp                     
   132 Olson                            121 Fripp                     
   133 Mallin                           122 Kaufling                  
   134 Rogers                           122 Kaufling                  
   135 Gee                              122 Kaufling                  
   136 Philtanker                       122 Kaufling                  
   137 Ladwig                           123 Vollman                   
   138 Stiles                           123 Vollman                   
   139 Seo                              123 Vollman                   
   140 Patel                            123 Vollman                   
   141 Rajs                             124 Mourgos                   
   142 Davies                           124 Mourgos                   
   143 Matos                            124 Mourgos                   
   144 Vargas                           124 Mourgos                   
   145 Russell                          100 King                      
   146 Partners                         100 King                      
   147 Errazuriz                        100 King                      
   148 Cambrault                        100 King                      
   149 Zlotkey                          100 King                      
   150 Tucker                           145 Russell                   
   151 Bernstein                        145 Russell                   
   152 Hall                             145 Russell                   
   153 Olsen                            145 Russell                   
   154 Cambrault                        145 Russell                   
   155 Tuvault                          145 Russell                   
   156 King                             146 Partners                  
   157 Sully                            146 Partners                  
   158 McEwen                           146 Partners                  
   159 Smith                            146 Partners                  
   160 Doran                            146 Partners                  
   161 Sewall                           146 Partners                  
   162 Vishney                          147 Errazuriz                 
   163 Greene                           147 Errazuriz                 
   164 Marvins                          147 Errazuriz                 
   165 Lee                              147 Errazuriz                 
   166 Ande                             147 Errazuriz                 
   167 Banda                            147 Errazuriz                 
   168 Ozer                             148 Cambrault                 
   169 Bloom                            148 Cambrault                 
   170 Fox                              148 Cambrault                 
   171 Smith                            148 Cambrault                 
   172 Bates                            148 Cambrault                 
   173 Kumar                            148 Cambrault                 
   174 Abel                             149 Zlotkey                   
   175 Hutton                           149 Zlotkey                   
   176 Taylor                           149 Zlotkey                   
   177 Livingston                       149 Zlotkey                   
   178 Grant                            149 Zlotkey                   
   179 Johnson                          149 Zlotkey                   
   180 Taylor                           120 Weiss                     
   181 Fleaur                           120 Weiss                     
   182 Sullivan                         120 Weiss                     
   183 Geoni                            120 Weiss                     
   184 Sarchand                         121 Fripp                     
   185 Bull                             121 Fripp                     
   186 Dellinger                        121 Fripp                     
   187 Cabrio                           121 Fripp                     
   188 Chung                            122 Kaufling                  
   189 Dilly                            122 Kaufling                  
   190 Gates                            122 Kaufling                  
   191 Perkins                          122 Kaufling                  
   192 Bell                             123 Vollman                   
   193 Everett                          123 Vollman                   
   194 McCain                           123 Vollman                   
   195 Jones                            123 Vollman                   
   196 Walsh                            124 Mourgos                   
   197 Feeney                           124 Mourgos                   
   198 OConnell                         124 Mourgos                   
   199 Grant                            124 Mourgos                   
   200 Whalen                           101 Kochhar                   
   201 Hartstein                        100 King                      
   202 Fay                              201 Hartstein                 
   203 Mavris                           101 Kochhar                   
   204 Baer                             101 Kochhar                   
   205 Higgins                          101 Kochhar                   
   206 Gietz                            205 Higgins                   
                                        162 Vishney                   
                                        133 Mallin                    
                                        136 Philtanker                
                                        154 Cambrault                 
                                        196 Walsh                     
                                        104 Ernst                     
                                        184 Sarchand                  
                                        172 Bates                     
                                        197 Feeney                    
                                        150 Tucker                    
                                        142 Davies                    
                                        143 Matos                     
                                        191 Perkins                   
                                        119 Colmenares                
                                        200 Whalen                    
                                        183 Geoni                     
                                        180 Taylor                    
                                        152 Hall                      
                                        137 Ladwig                    
                                        139 Seo                       
                                        126 Mikkilineni               
                                        125 Nayer                     
                                        170 Fox                       
                                        175 Hutton                    
                                        129 Bissot                    
                                        163 Greene                    
                                        105 Austin                    
                                        176 Taylor                    
                                        188 Chung                     
                                        116 Baida                     
                                        115 Khoo                      
                                        144 Vargas                    
                                        195 Jones                     
                                        174 Abel                      
                                        157 Sully                     
                                        182 Sullivan                  
                                        156 King                      
                                        194 McCain                    
                                        193 Everett                   
                                        187 Cabrio                    
                                        117 Tobias                    
                                        179 Johnson                   
                                        135 Gee                       
                                        159 Smith                     
                                        131 Marlow                    
                                        190 Gates                     
                                        169 Bloom                     
                                        166 Ande                      
                                        151 Bernstein                 
                                        204 Baer                      
                                        203 Mavris                    
                                        160 Doran                     
                                        155 Tuvault                   
                                        107 Lorentz                   
                                        185 Bull                      
                                        128 Markle                    
                                        134 Rogers                    
                                        140 Patel                     
                                        168 Ozer                      
                                        178 Grant                     
                                        141 Rajs                      
                                        181 Fleaur                    
                                        165 Lee                       
                                        138 Stiles                    
                                        173 Kumar                     
                                        206 Gietz                     
                                        164 Marvins                   
                                        202 Fay                       
                                        112 Urman                     
                                        189 Dilly                     
                                        110 Chen                      
                                        153 Olsen                     
                                        161 Sewall                    
                                        186 Dellinger                 
                                        109 Faviet                    
                                        177 Livingston                
                                        198 OConnell                  
                                        106 Pataballa                 
                                        111 Sciarra                   
                                        118 Himuro                    
                                        132 Olson                     
                                        192 Bell                      
                                        113 Popp                      
                                        171 Smith                     
                                        127 Landry                    
                                        167 Banda                     
                                        130 Atkinson                  
                                        158 McEwen                    
                                        199 Grant                     

 195 rows selected 

1 个答案:

答案 0 :(得分:2)

在我看来,right join的查询会让经理和员工感到困惑。这就是right join似乎无法回答明确答案的原因。使用left join,您需要"所有员工,如果有相关经理,也需要经理"。使用right join,无论他们是否是经理,您仍然可以获得"所有员工和#34;所以"右边的意思"是错的。

当然,表格的目的是包含两种类型,但可能通过更好的分离可以获得更清晰的图像。

说,经理是没有manager_id的所有人(如果你的桌子不是深树,那就是这种情况)。然后,看看这个修改:

SELECT emps.employee_id as "Employee", emps.last_name, mgr.employee_id as "Manager", mgr.last_name
FROM employees emps
RIGHT JOIN (SELECT * FROM employees WHERE manager_id IS NULL) mgr
ON emps.manager_id = mgr.employee_id 

像这样,right join的数据基础将是所有经理的正确选择。即使他们没有员工,他们也会向员工展示。然而,这最后一次"甚至"你所选择的那种关系不会发生。

然后,我完全同意@Ameya Desphande有一个身份证号为156的第二位国王。这更令人费解; - )