How to Query Supervisor-Subordinate Relationships in SQL
I apologize for any confusion, but there seems to be an error in the query you provided. It appears that the Supervisor column is not defined, and there is an unnecessary semicolon and extra line break in the query. Additionally, the WHERE clause is not correctly comparing the employee numbers.
Here's an updated query that should fulfill your requirements:
SELECT E1.empno AS Supervisor, E1.ename AS SupervisorName, E2.empno AS Subordinate, E2.ename AS SubordinateName
FROM Emp E1, Emp E2
WHERE E2.mgr = E1.empno
ORDER BY E1.empno ASC, E2.empno ASC;
In the above query, it assumes that the mgr column in the Emp table represents the employee number of the supervisor. Adjust the column names (empno, ename, mgr) according to your specific table structure.
This corrected SQL query uses a self-join on the 'Emp' table to establish the relationship between supervisors and their subordinates. The 'WHERE' clause ensures that the 'mgr' column of the subordinate matches the 'empno' of the supervisor. The results are then ordered for clear hierarchical presentation.
Please let me know if you need any further assistance!
原文地址: https://www.cveoy.top/t/topic/FAO 著作权归作者所有。请勿转载和采集!