简体   繁体   中英

Self-join: SQL parse error on table alias

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM