使用相同的外键连接多个表

时间:2014-08-29 18:20:51

标签: mysql sql join

'我有一个数据库,员工被分配到不同的项目。有些项目分为不同的任务。已创建以下数据库结构。'

USER:
+------------+-------------+
| Person ID  | Name        | 
+------------+-------------+
| 1          | A           | 
| 2          | B           | 
| 3          | C           | 
+------------+-------------+
PROJECT_ASSIGNMENT:
+-----------------+-------------+-----------+
| P_Assign key    | Project Key | Person ID |
+-----------------+-------------+-----------+
| 1               | 1           | 1         | 
| 2               | 1           | 2         |     
| 3               | 1           | 3         |    
+------------+-------------+------+-----+----
TASK ASSIGNMENT:
+-----------------+-------------+-----------+--------+
| T_Assign key    | Project Key | Person ID |task key|
+-----------------+-------------+-----------+--------+
| 1               | 2           | 1         | 1      |
| 2               | 2           | 2         | 1      |
| 3               | 2           | 3         | 1      |
| 4               | 2           | 1         | 2      |
| 5               | 2           | 2         | 2      |
| 6               | 2           | 3         | 2      |
+------------+-------------+------+-----+------------+;

'What type of query would be required to get results like this?'

+-----------------+-------------+-----------+
| Person ID       | Project Key | Task Key  |
+-----------------+-------------+-----------+
| 1               | 1           | NULL      | 
| 1               | 2           | 1         | 
| 1               | 2           | 2         |
| 2               | 1           | NULL      |
| 2               | 2           | 1         | 
| 2               | 2           | 2         |
| 3               | 1           | NULL      |
| 3               | 2           | 1         | 
| 3               | 2           | 2         |
+------------+-------------+------+-----+---+

我尝试了一些查询,但我认为存在相同外键的事实阻止了我获得所需的结果。

SELECT 
 user.person_ID, 
 task_assignment.Project Key, 
 project_assignment.Project Key
FROM user
JOIN dbo.project_assignment 
     ON project_assignment.person_key=user.person_key
LEFT JOIN dbo.task_assignment 
     ON dbo.project_assignment.person_key=dbo.task_assignment.person_key

如果将项目拆分为任务,则会将该人员分配给该任务。 如果项目没有拆分为任务,则该人员将被分配到项目中。

4 个答案:

答案 0 :(得分:0)

即使你没有真正表现出你所做的一切。此方案的示例查询:

SELECT p.P_Assign key, p.Project Key, p.Person I FROM PROJECT_ASSIGNMENT p
INNER JOIN TASK ASSIGNMENT t
ON p.Person_ID=t.Person_ID

假设person_id是project_assignment的主键和任务分配的外键。

这将加入前两个。在select语句中,指定要在结果中显示的列。

答案 1 :(得分:0)

我实际上并没有想到你想要的东西,但我想你可能正在寻找这个问题:

SELECT PersonId, ProjectKey, TaskKey
FROM TASK_ASSIGNMENT
GROUP BY PersonId, ProjectKey, TaskKey;

希望这有帮助。

答案 2 :(得分:0)

鉴于样本数据,为了获得相同的结果,我们需要组合两个查询。

第一个查询:

SELECT u.person_id
     , p.project_key
     , t.task_key
  FROM user u
  JOIN project_assignment p
    ON p.person_id = u.person_id
  LEFT
  JOIN task_assignment t
    ON t.person_id   = p.person_id
   AND t.project_key = p.project_key
 ORDER 
    BY u.person_id
     , p.project_key

将返回:

person_id  project_key  task_key
---------  -----------  --------
1          1            NULL
2          1            NULL
3          1            NULL

第二个问题:

SELECT u.person_id
     , t.project_key
     , t.task_key
  FROM user u
  JOIN task_assignment t
    ON t.person_id   = u.person_id
 ORDER 
    BY u.person_id     
     , t.project_key
     , t.task_key

将返回:

person_id  project_key  task_key
---------  -----------  --------
1          2            1
1          2            2
2          2            1
2          2            2
3          2            1
3          2            2

这两个查询的结果可以与UNION ALL集合运算符组合,然后添加ORDER BY子句,例如:

SELECT u.person_id
     , p.project_key
     , t.task_key
  FROM user u
  JOIN project_assignment p
    ON p.person_id = u.person_id
  LEFT
  JOIN task_assignment t
    ON t.person_id   = p.person_id
   AND t.project_key = p.project_key
 UNION ALL
SELECT u.person_id
     , t.project_key
     , t.task_key
  FROM user u
  JOIN task_assignment t
    ON t.person_id   = u.person_id
 ORDER 
    BY person_id
     , project_key
     , task_key IS NOT NULL
     , task_key

示例数据中没有涉及某些情况......如果给定用户在project_assignment表和task_assignment表中都出现相同的项目键,该怎么办?输出应该是什么样的? (可能会对查询进行一些调整。)

答案 3 :(得分:0)

尝试如下....

Select 
User.Person_ID,
all_tasks.Project_key,
all_tasks.Task_Key
From User 
Left Join  
(
Select Person_ID, Project_Key, Null as Task_Key From Project_Assignment
Union All
Select Person_ID, Project_key, Task_Key From Task_Assignment
) all_tasks On all_tasks.person_ID = User.Person_ID