我有一个这样的表结构(无法更改):
CREATE TABLE people(id INT PRIMARY KEY, name VARCHAR(150));
CREATE TABLE positions(id INT PRIMARY KEY, name VARCHAR(100));
CREATE TABLE positionAssignments(id INT PRIMARY KEY, fkPosition INT,
fkAssignedPerson INT, fkAssignedPosition INT, startDate DATETIME,
endDate DATETIME,
FOREIGN KEY(fkAssignedPerson) REFERENCES people(id),
FOREIGN KEY(fkAssignedPosition REFERENCES positions(id));
处于positionAssignments
:
fkPosition
是分配给该人的哪个职位,或另一个职位。 fkAssignedPerson
被分配到该职位的人员。fkAssignedPosition
分配给另一个的职位。 我的问题是我需要获得一个人的所有职位。由于此结构可以递归,因此我需要创建一个返回5级深度的SQL。数据可以如下:
people positions
| id | name | | id | name |
+----+---------+ +----+-------------+
| 1 | Alice | | 10 | Position 1 |
+----+---------+ +----+-------------+
| 2 | Bob | | 20 | Position 2 |
+----+---------+ +----+-------------+
| 3 | Charlie | | 30 | Position 3 |
+----+---------+ +----+-------------+
| 4 | Daniel | | 40 | Position 4 |
+----+---------+ +----+-------------+
| 50 | Position 5 |
+----+-------------+
| 60 | Position 6 |
+----+-------------+
positionAssignments
| id | fkPosition | fkAssignedPerson | fkAssignedPosition| startDate | endDate |
+----+------------+------------------+-------------------+------------+---------+
| 1 | 10 | 1 | null | 2018-01-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 2 | 20 | 2 | null | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 3 | 20 | null | 30 | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 4 | 30 | 3 | null | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 5 | 40 | null | 20 | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 6 | 50 | null | 40 | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 7 | 60 | null | 50 | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 8 | 60 | null | 10 | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
| 9 | 60 | 4 | null | 2018-02-01 | null |
+----+------------+------------------+-------------------+------------+---------+
在这种结构中,爱丽丝属于位置1,鲍勃属于位置2,查理属于位置2和3。
我的问题:我已经完成了一个SQL,使用UNION
来获取一个人的所有职位,就像这样:
SELECT pe.id, pa1.fkPosition, pa1.id, pa1.startDate, pa1.endDate
FROM people pe
INNER JOIN positionAssignments pa1
ON pe.id = pa1.fkAssignedPerson
UNION
SELECT pe.id, pa2.fkPosition, pa2.id, pa2.startDate, pa2.endDate
FROM people pe
INNER JOIN positionAssignments pa1
ON pe.id = pa1.fkAssignedPerson
INNER JOIN positionAssignments pa2
ON pa1.fkPosition = pa2.fkAssignedPosition
-- And so on...
还有什么更好的方法,请记住我使用MySQL 5.5,并且希望将其存储在视图中(应该使用 merge 算法)?
所需的输出将是:
Expected Result
| idPerson | idPosition | idAssignment | startDate | endDate |
+----------+------------+--------------+------------+---------+
| 1 | 10 | 1 | 2018-01-01 | null |
+----------+------------+--------------+------------+---------+
| 1 | 60 | 8 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 2 | 20 | 2 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 2 | 40 | 5 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 2 | 50 | 6 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 2 | 60 | 7 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 3 | 20 | 3 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 3 | 30 | 4 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 3 | 40 | 5 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 3 | 50 | 6 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 3 | 60 | 7 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
| 4 | 60 | 9 | 2018-02-01 | null |
+----------+------------+--------------+------------+---------+
编辑:添加了新的测试数据