1

I have a problem about database table and query of it. Let me tell what this is about. It is about project-manager-employee relationship. Now Let me tell in statements what their relationship...

Manager = Manager is a project leader or could be manager of MORE than ONE project.

Employee = (s)He is a person who only works on project or projectS which is managed by Manager.

Project= Project is a project which managed by only ONE Manager.

Employee - Manager Relationship

Manager is also a employee in company right? According to this I want their relationship like -> one Employee can be managed by more than ONE Manager.
All managers are in the same title I mean there is no manager of a manager. There is only manager(s) for an employee. For example 5 employees can be managed by a Manager.

Employee - Project Relationship

Employee can have a project that make sense, also employee can have more than ONE project that's make sense too. So project can have more than ONE employee too. (reminder: Project can have more than ONE Employee, BUT can NOT be managed by more than ONE Manager).

Manager - Project Relationship

Manager can have more than ONE project. BUT Project can NOT be managed by more than ONE Manager.

PS: According to project, same employee in different projects CAN BE managed by more than one manager See my example below...

Android App PROJECT's Manager----> Patrick

Android App PROJECT's Employees-----> John, Susan, Megan, Joe.

Another Project...

IOS App PROJECT's Manager----> Ryan

IOS App PROJECT's Employees----> Claire, Walter, Robert, Megan

Now let me show my tables for that... I can't really know what should I do...

Employee-Manager-Project current realationship

In that table I tried to do that "manager_id(FK)" refer to "employee_id(PK)" because Manager is a employee in company.

And I have "manager_id(FK)" in Project table to refers "manager_id" in employee table.

And which is proj_employee table I didn't clearly understand. I found this way in examples from internet. That way we call them in query.

NOW let me post my queries I tried but never get a solution...

        select
    e.name as employee_name,
    pm.name as manager_name
from Proj_Employee as pe
    left outer join Employee as e on e.employee_id = pe.emp_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>

I would like to ask this... I want a query which shows me EmployeeName, ManagerName OF choosen project. And if my table is wrong Could you guide me or tell me how can I do that?

I have knowledge but couldn't figure it out how can I make this work.

4

3 回答 3

2

我想我看到了问题(最后)。

你说,“我在 Project 表中有“manager_id(FK)”来引用员工表中的“manager_id”。

我认为这可能是问题所在。我认为您希望manager_id在表中成为表中列project的外键,而不是列。但是您在查询中得到了正确的结果。employee_idemployeemanager_id


SQL Fiddle 演示在这里:http://www.sqlfiddle.com/#!2/e31fb/1


与您的问题并不真正相关,但我只是想借此机会指出,在每个命名的表中都有主键列id有助于我们使“错误”SQL 看起来像“错误”SQL。“让错误的代码看起来是错误的”——乔尔·斯波尔斯基

如果我们期望:

主键将采用以下形式: id

外键将采用以下形式: parent_table_id, 或parent_role_id

绝大多数连接都是从外键到主键

那么我们期望大多数连接将采用以下形式

child.parent_id = parent.id

并且外键定义的形式为

FK_child_parent_or_role (parent_or_role_id) REFERENCES parent (id)

这使得外键定义如下:

FK_project_manager (manager_id) REFERENCES employee (manager_id)

在我们看来“错了”。我们希望看到对id列的引用。

于 2013-08-21T18:09:16.840 回答
0

没有理由有我可以看到的外部连接。

select p.Project_Name, m.name as Manager, e.name as Employee
from Project P
join Employee M on m.employee_id = p.manager_id
join project_employee pe on pe.project_id = p.project_id
join Employee E on e.employee_id = pe.employee_id
于 2013-08-21T18:02:11.787 回答
0

您可以使用以下内容获取 (project_name, manager_name, employee_name) 的列表:

SELECT p.project_name
     , e.name as employee_name
     , m.name as manager_name
FROM project p
INNER JOIN employee m ON p.manager_id = m.employee_id
LEFT JOIN proj_employee pe ON p.project_id = pe.project_id
LEFT JOIN employees e ON pe.emp_id = e.employee_id;

你会在两个项目中得到类似的东西,一个没有员工:

 project_name  | employee_name | manager_name 
---------------+---------------+--------------
 Stack O       | Alice         | Eve
 Stack O       | Bob           | Eve
 Lemon Squeezy |               | Alice
于 2013-08-21T17:54:17.093 回答