Not quite understanding the query after just shifting column names

白昼怎懂夜的黑 提交于 2019-12-24 00:58:37

问题


This is my table tblEmployee ,when I'm trying to use a self join query it's result is varying while shifting columns, can someone please explain me the difference between those two queries.Thanks

EmployeeID  Name    ManagerID
 1          Mike     3
 2          Rob      1
 3          Todd    NULL
 5          Ben      1
 6          Sam      1

Query_1

SELECT E.Name AS Employee, M.Name AS Manager
from tblEmployee E
LEFT JOIN tblEmployee M
on E.ManagerID = M.EmployeeID  

OUTPUT

Employee    Manager
 Mike        Todd
 Rob         Mike
 Todd        NULL
 Ben         Mike
 Sam         Mike

QUERY_2

SELECT E.Name AS Employee, M.Name AS Manager
from tblEmployee E
LEFT JOIN tblEmployee M
on E.EmployeeID = M.ManagerID  

OUTPUT

Employee  Manager
Mike       Rob
Mike       Ben
Mike       Sam
Rob        NULL
Todd       Mike
Ben        NULL
Sam        NULL

回答1:


First a simple explanation: the left join takes every row in the first table that has at least a match in the second table on the column you selected.

In the first query you are considering the Manager ID on every row and looking for a match on the Employee ID in the second table: since value in the second table (Employee ID) is a key (at least in the example definition), you can find only one row that matches that ID.

Eg: First row Mike has Manager ID = 3, when you are looking at the table you have only one row that has Employee ID = 3, and that is Todd

In the second query you are considering the Employee ID on every row and looking for a match on the Manager ID in the second table: since there could be more than one row with the same Manager ID, you will receive more value for every row.

Eg: First row Mike has Employee ID = 1, when you are looking at the table you have three rows with anager ID = 1 and those are Rob, Ben and Sam.




回答2:


With Query1 you are selecting each employee (E) and their manager (M) if there is one (left join).

With Query2 you reversed that. You are selecting each employee (E) and their subordinates (M). The alias for M.Name is incorrect now, because it's not the manager but a subordinate. As some employees have more than one subordinate, they are listed once for each of them (Mike).




回答3:


Basically the second query is little out of order in the sense that since you have swapped the join criteria columns, you get

all combination of manager name followed by the employee name they are manager of

unlike the first query which gave you

all combination of employee names followed by their manager name

So your corrected 2 query should be

SELECT E.Name AS Manager, M.Name AS Employee -- note changes here
from tblEmployee E
LEFT JOIN tblEmployee M
on E.EmployeeID = M.ManagerID  

OUTPUT

Manager Employee

Mike       Rob
Mike       Ben
Mike       Sam
Rob        NULL
Todd       Mike
Ben        NULL
Sam        NULL

Or maybe you wanted the same result the other way round

then your query should be using RIGHT JOIN instead of LEFT JOIN

SELECT E.Name AS Employee, M.Name AS Manager
from tblEmployee E
RIGHT JOIN tblEmployee M -- note the change in JOIN
on E.EmployeeID = M.ManagerID  

OUTPUT

Employee Manager

 Todd        Mike   
 Mike        Rob    
 NULL        Todd   
 Mike        Ben    
 Mike        Sam    


来源:https://stackoverflow.com/questions/36615531/not-quite-understanding-the-query-after-just-shifting-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!