这是一个想法。一个人的名字和一个他们合作过的所有人的逗号分隔列表怎么样?这是 MySQL 中的语法:
SELECT pp.p1, pp.p1.name, group_concat(distinct p2.name)
FROM (select p1.id as p1, p1.name as p1name, p2.id as p2, p2.name as p2name
from person p1 cross join
person p2
where p1.id <> p2.id
) pp join
project pr1
on pr1.personid = pp.p1 join
project pr2
on pr2.personid = pp.p2 and
pr2.id = pr1.id
group by pp.p1, pp.p1name;
这个想法从所有成对的人的主列表开始。然后它加入第一个人参与的所有项目,以及第二个人参与的所有匹配项目。这些连接在一起,使用group concat
.
如果您对这些对的列表感到满意,下面是一个在 SQL Server 中运行的示例:
with person as (
select 1 as id, 'a' as name union all
select 2, 'b' union all
select 3, 'c' union all
select 4, 'd' union all
select 5, 'e'
),
project as (
select 1 as id, 1 as personid union all
select 1 as id, 2 as personid union all
select 1 as id, 3 union all
select 2, 4 union all
select 2, 5 union all
select 3, 1 union all
select 3, 5
)
SELECT distinct pp.p1name, pp.p2name
FROM (select p1.id as p1, p1.name as p1name, p2.id as p2, p2.name as p2name
from person p1 cross join
person p2
where p1.id <> p2.id
) pp join
project pr1
on pr1.personid = pp.p1 join
project pr2
on pr2.personid = pp.p2 and
pr2.id = pr1.id;
编辑:
这是思考问题的另一种方式(如果我的解释是正确的)。我不知道为什么我没有先朝这个方向走。您正在寻找在一个项目上一起工作的所有(不同的)人。从这样的列表中获取对是自联接。这导致以下查询:
SELECT distinct p1.name, p2.name
FROM project pr1 join
project pr2
on pr1.id = pr2.id and
pr1.personid <> pr2.personid join
person p1
on pr1.personid = p1.id join
person p2
on pr2.personid = p2.id;
(group_concat()
如果您想将行放入逗号分隔的列表中,可以添加。)