SQL查询子查询结构

时间:2014-11-24 19:56:10

标签: mysql sql-server heidisql

我遇到了SQL问题。我创建了一个餐馆数据库,我正在尝试创建查询,显示为同一客户提供服务的主管和员工。

Employee表格有supervisor_idemployee_idOrder表格中包含与该客户相关联的customer_idemployee_id。我知道这涉及一个子查询,我知道它会看起来像这样。

SELECT DISTINCT
        Employee.employee_id,
        Orders.customer_id,
        Employee.supervisor_id
FROM    Employee,
        Orders
WHERE   EXISTS ( SELECT customer_id,
                        Employee.employee_id
                 FROM   Orders AS cs2,
                        Employee
                 WHERE  cs2.employee_id = Employee.employee_id
                        AND Employee.supervisor_id = Employee.employee_id
                        AND cs2.customer_id = Orders.customer_id );

截至目前,此查询未返回任何结果。

1 个答案:

答案 0 :(得分:0)

可能是这样的:

SELECT a.employee_id, b.customer_id, a.supervisor_id
FROM   Employee a INNER JOIN Orders b ON a.employee_id = b.employee_id 
       INNER JOIN Orders c ON b.customer_id = c.customer_id
       AND c.employee_id = a.supervisor_id