查找与经理住在相同城市和街道的所有员工

时间:2018-10-01 19:07:14

标签: sql postgresql

关系数据库是

employee(employee_name, street, city)
works(employee_name company_name, salary)
company(company_name, city)
manages(employee_name, manager_name)

,我需要找到所有与经理住在同一条街道和城市的员工。我的代码如下:

select distinct employee_name
from employee, company
where employee.city = company.city;

任何建议或技巧都很好。

1 个答案:

答案 0 :(得分:1)

尝试一下

SELECT e.employee_name
FROM  employee e
JOIN manages m ON m.employee_name = e.employee_name
WHERE EXISTS (
    SELECT 1
    FROM empoloyee 
    WHERE employee_name = m.manager_name
        AND street = e.street
        AND city = e.city
)