Question

Using a simple self join to list employees' managers:

CREATE VIEW AS 
SELECT e1.EMP_ID EmployeeId, e1.FNAME EmployeeName,  
       e1.MANAGER ManagerName
FROM   EMPLOYEE e1 
       LEFT JOIN EMPLOYEE e2 
       ON e1.MANAGER = e2.EMP_ID

Where the table in question is EMPLOYEE, with the primary key EMP_ID.

Both MySQL and Oracle return errors for the code; and although I have tried a number of different variations, the main stumbling block is the use of an alias for the table in question (e1 and e2) which neither dbms consider to be legal identifiers.

Était-ce utile?

La solution

You need a name for your view:

CREATE VIEW v_emp AS 
SELECT e1.EMP_ID EmployeeId, e1.FNAME EmployeeName,  
       e1.MANAGER ManagerName
FROM   EMPLOYEE e1 
       LEFT JOIN EMPLOYEE e2 
       ON e1.MANAGER = e2.EMP_ID
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top