我需要检索不同的project_name,其中任何worker_id及其supervisor_id为同一个项目工作。我知道答案是bb,dd和ee,但不知道要检索它的查询。这是我目前的查询:
SELECT project.project_name, worker.supervisor_id, projectworker.worker_id
FROM project JOIN projectworker
on project.project_id = projectworker.project_id
JOIN worker
on worker.worker_id = projectworker.worker_id
order by project_name;
检索:
project_name | supervisor_id | worker_id
aa 4 1
aa 4 1
aa 4 2
aa 4 3
bb 12 5
bb NULL 12
dd 4 3
dd 4 6
dd 3 7
dd 4 3
ee NULL 12
ee 4 1
ee 2 8
ee 1 9
ee 9 10
答案 0 :(得分:1)
这是一种方法。
SELECT project.project_name
FROM project
JOIN projectworker
on project.project_id = projectworker.project_id
JOIN worker
on worker.worker_id = projectworker.worker_id
WHERE EXISTS (SELECT 1
FROM projectworker
WHERE project_id = project.project_id AND worker_id = worker.supervisor_id)
GROUP BY project.project_name
ORDER BY project_name;
答案 1 :(得分:0)
试试这个:
SELECT distinct project.project_name, worker.supervisor_id, projectworker.worker_id
FROM project JOIN projectworker
on project.project_id = projectworker.project_id
JOIN worker
on worker.worker_id = projectworker.worker_id
where worker.project_id = projectworker.project_id
order by project_name;