小编典典

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

sql

关系数据库是

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;

任何建议或技巧都很好。


阅读 230

收藏
2021-04-14

共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
)
2021-04-14