3

我做了一些挖掘,但无法在脑海中安排......我的问题是找到在同一个项目中的员工和经理?我有 2 个表,它们是 Employee 和 Project。他们喜欢;

   *Employee*               
   ----------               
   employee_id (PK)          
   name
   manager_id (FK)   ref employee_id in same table which is employee_id.
   usertype    

   *Project*
   ----------
   project_id(PK)
   project_name
   employee_id(FK)   ref employee_id in emplyee table
   manager_id(FK)    ref employee_id in emplyee table

让我更具体。项目必须有 1 名经理和多名员工。我怎样才能写查询?usertype 列指定此员工 MANAGER OR USER(普通员工)。

查询运行示例:

       ______    ______________    ____________
       |name|    |manager_name|   |project_name|  
        John         Susan       CalendarIphone
        Mike         Susan       CalendarIphone
        Joe         Patrick        AndoridApp
        Megan        Susan        CalendarIphone
        Melek       Patrick        AndoridApp

如您所见,Mike 可以参与多个项目,并且可以拥有多个经理。但经理必须只拥有 1 个项目。那么我该怎么做才能得到这个呢?预先感谢。

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ------------------------------

我有一个有下拉列表的项目。我在这里选择了一个项目,它根据该项目向我显示了一个表格,并列出了该项目的经理,该项目中的员工。我问了项目名称,只需再创建一列,您可以忽略它。我只想看看它的经理和里面的员工。

编辑 2------------------------------------------------ ------------

例如,项目 ID = 2,在这个项目中,我们有 1 名经理和员工。

   |name|    |manager_name|    
    John         Susan      
    Mike         Susan       
    Joe          Susan       
    Megan        Susan       
    Melek        Susan
4

2 回答 2

4

你的架构不好。它应该是这样的:

   Employee*               
   ----------               
   employee_id (PK)          
   name
   manager_id (FK)   ref employee_id in employee table

   *Project*
   ----------
   project_id(PK)
   project_name
   manager_id(FK)    ref employee_id in employee table

   *Project_Employee*
   ----------
   project_id(FK)  ref project_id in project table
   employee_id(FK) ref employee_id in employee table

那么你可以这样做:

   select
       e.name as name,
       em.name as manager_name
       p.project_name
       pm.name as project_manager_name
   from Employee as e
       left outer join Employee as em on em.employee_id = e.manager_id
       left outer join Project_Employee as pe on pe.employee_id = e.employee_id
       left outer join Project as p on p.project_id = pe.project_id
       left outer join Employee as pm on pm.employee_id = p.manager_id

但是对于每个员工可能有多个项目,员工可以在此查询中重复

更新

 select
     e.name, u.is_manager
 from (
     select employee_id, 0 as is_manager from Project_Employee as pe where pe.idproject_id= <project id>
     union all
     select manager_id, 1 as is_manager from Project as p where p.project_id= <project id>
 ) as u
     inner join Employee as e on e.employee_id = u.employee_id
 order by u.is_manager desc

更新 2

select
    e.name as employee_name,
    pm.name as manager_name
from Project_Employee as pe
    left outer join Employee as e on e.employee_id = pe.employee_id
    left outer join Project as p on p.project_id = pe.project_id
    left outer join Employee as pm on pm.employee_id = p.manager_id
where pe.project_id = <project id>
于 2013-08-21T12:04:22.277 回答
0
SELECT e.name AS name,
       m.name AS manager_name,
       project_name
FROM Project p
    JOIN Employee e ON e.employee_id = p.employee_id
    JOIN Employee m ON m.employee_id = p.manager_id

由于每个项目行都有员工和经理,因此只需Employee使用两个不同的键访问表格即可获取员工姓名和经理姓名。然后,很自然地将查询基于 Project,因为这是您正在收集的信息,因此您可以project_name从那里获取信息。

于 2013-08-21T12:00:43.787 回答