2014-11-24 132 views
0

我遇到SQL問題。我創建了一家餐廳的數據庫,並且我試圖創建查詢,顯示曾經服務過同一客戶的主管和員工。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

這是MS SQL Server或MySQL的?他們是不同的。 – 2014-11-24 19:57:25

+0

這是Heidi SQL – Emma 2014-11-24 21:31:52

回答

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