10

您好,我有一个包含以下列的员工表

Emp_id, Emp_Name and Mgr_id.

我正在尝试创建一个将列出的视图

Emp_id, Emp_name, Mgr_id and Mgr_name(通过交叉连接 Employee 表)。我尝试了外部联接、内部联接等,但我无法正确处理。

非常感谢任何帮助。

CREATE TABLE [dbo].[tblEmployeeDetails](
[emp_id] [bigint] NOT NULL, 
[emp_name] [nvarchar](200) NULL,    
[emp_mgr_id] [bigint] NULL, CONSTRAINT [PK_tblEmployeeDetails] PRIMARY KEY CLUSTERED (
[emp_id] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY]
4

15 回答 15

21
CREATE VIEW AS
SELECT e1.emp_Id EmployeeId, e1.emp_name EmployeeName, 
       e1.emp_mgr_id ManagerId, e2.emp_name AS ManagerName
FROM   tblEmployeeDetails e1
       JOIN tblEmployeeDetails e2
       ON e1.emp_mgr_id = e2.emp_id

编辑:如果 emp_mgr_id 为空,Left Join 将起作用。

CREATE VIEW AS 
SELECT e1.emp_Id EmployeeId, e1.emp_name EmployeeName,  
       e1.emp_mgr_id ManagerId, e2.emp_name AS ManagerName 
FROM   tblEmployeeDetails e1 
       LEFT JOIN tblEmployeeDetails e2 
       ON e1.emp_mgr_id = e2.emp_id
于 2012-07-11T07:13:09.787 回答
7
   SELECT b.Emp_id, b.Emp_name,e.emp_id as managerID, e.emp_name as managerName
    FROM Employee b
    JOIN Employee e ON b.Emp_ID = e.emp_mgr_id

试试这个,它本身就是一个 JOIN 来获得经理:)

于 2012-07-11T07:11:00.843 回答
4
SELECT e1.empno EmployeeId, e1.ename EmployeeName, 
       e1.mgr ManagerId, e2.ename AS ManagerName
FROM   emp e1, emp e2
       where e1.mgr = e2.empno
于 2014-01-22T18:32:29.243 回答
4
CREATE VIEW EmployeeWithManager AS 
SELECT e.[emp id], e.[emp name], m.[emp id], m.[emp name] 
FROM Employee e LEFT JOIN Employee m ON e.[emp mgr id] = m.[emp id]

此定义使用左外连接,这意味着即使经理 ID 为 NULL 或经理已被删除(如果您的应用程序允许)的员工也会被列出,他们的经理属性返回为 NULL。

如果您改为使用内部联接,则只会列出拥有经理的人员。

于 2012-07-11T07:26:15.037 回答
3

正如杰西所说,使用自我加入:

SELECT 
  e.emp_id
  , e.emp_name
  , e.emp_mgr_id
  , m.emp_name AS mgr_name 
FROM [dbo].[tblEmployeeDetails] e 
LEFT JOIN [dbo].[tblEmployeeDetails] m ON e.emp_mgr_id = m.emp_id
于 2012-07-11T07:13:59.697 回答
3
    select E1.emp_id [Emp_id],E1.emp_name [Emp_name],
E2.emp_mgr_id [Mgr_id],E2.emp_name [Mgr_name] 
from [tblEmployeeDetails] E1 left outer join
    [tblEmployeeDetails] E2
    on E1.emp_mgr_id=E2.emp_id
于 2012-07-11T07:18:14.183 回答
3

试试这个。

SELECT Employee.emp_id, Employee.emp_name,Manager.emp_id as Mgr_Id, Manager.emp_name as Mgr_Name 
FROM tblEmployeeDetails Employee 
LEFT JOIN tblEmployeeDetails Manager ON Employee.emp_mgr_id = Manager.emp_id
于 2012-07-11T07:18:48.900 回答
2

试试这个..你应该做LEFT JOIN的是忽略表中的空值

SELECT a.emp_Id EmployeeId, a.emp_name EmployeeName,  
       a.emp_mgr_id ManagerId, b.emp_name AS ManagerName 
FROM   tblEmployeeDetails a 
       LEFT JOIN tblEmployeeDetails b
       ON b.emp_mgr_id = b.emp_id
于 2012-07-20T01:58:21.043 回答
2
TableName :Manager

EmpId   EmpName     ManagerId
1       Monib       4

2       zahir       1

3       Sarfudding  NULL

4       Aslam       3


select e.EmpId as EmployeeId,e.EmpName as EmployeeName,e.ManagerId as ManagerId,e1.EmpName as Managername from Manager e
  join manager e1 on e.ManagerId=e1.empId
于 2014-09-04T14:53:02.540 回答
1
create table abc(emp_ID int, manager varchar(20) , manager_id int)

emp_ID  manager manager_id
1       abc     NULL
2       def     1
3       ghi     2
4       klm     3
5       def1    1
6       ghi1    2
7       klm1    3

select a.emp_ID , a.manager emp_name,b.manager manager_name
from abc a
left join abc b
on a.manager_id = b.emp_ID

Result:
emp_ID  emp_name  manager_name
1       abc       NULL
2       def       abc
3       ghi       def
4       klm       ghi
5       def1      abc
6       ghi1      def
7       klm1      ghi
于 2016-08-26T16:00:01.400 回答
0
select E1.EmpId,E1.Name,E2.Name as Manager from Employee E1 left join Employee E2 on  E1.ManagerID = E2.EmpId
于 2014-03-28T01:44:33.990 回答
0
create view as 
(select 
e1.empno as PersonID,
e1.ename as PersonName,
e2.empno MANAGER_ID,
e2.ename MANAGER_NAME 
from 
employees e1 , employees e2 
where 
e2.empno=e1.mgr)
于 2014-01-21T14:27:55.380 回答
0

此外,您可能希望让经理和他们的报告计数 -

SELECT e2.ename ,count(e1.ename)  FROM employee_s e1 LEFT OUTER JOIN employee_s e2 
ON e1.manager_id = e2.eid
group by e2.ename;
于 2016-08-14T07:21:42.780 回答
0
SELECT e1.emp_id, e1.emp_name, e1.mgr_id, e2.emp_name as manager_name

FROM employee e1

JOIN employee e2

ON e1.mgr_id = e2.emp_id

ORDER BY e1.emp_id

*这是指向 SQL Fiddle 的链接以及一个工作示例。 http://www.sqlfiddle.com/#!17/392b5/9

于 2019-01-19T07:21:40.170 回答
0
select distinct( e1.id), e1.emp_name from employee as e1 
inner join employee as e2 on e1.id = e2.manager_id;
于 2022-02-08T12:06:05.113 回答