SQL query to obtain pair links
I have three tables:
- one 开发者_JAVA技巧indexed by the names of projects (PROJECT)
- one indexed by the names of people (PARTICIPANT)
one that links members to projects (one row for each member of each project) (PROJPART).
People may, and often will, be a member of more than one project. I need a SQL query (using MySQL) that will list all pairs of projects that are linked because they share at least one member. Is this possible in SQL? If so, can you suggest the form of a query?
select c.project_name, d.project_name
from projpart a
inner join projpart b
on a.memberid = b.memberid
and a.projectid < b.projectid
inner join project c on c.projectid = a.projectid
inner join project d on d.projectid = b.projectid
The inner select will return the members which in more than 1 project. Those project will be connected.
SELECT member_id,project FROM PROJECT, PROJPART WHERE
PROJPART.project_id= PROJECT.project_id AND
PROJPART.MEMBER IN
(SELECT MEMBER, SUM(1) FROM PROJPART
GROUP BY members
HAVING SUM(1)>1)
ORDER BY member_id
I think it is not exactly the same what you want but it might help you
精彩评论