Either just use the original column name - this should work as long as the MANAGERS
table does not have an ID
column:
SELECT NAME, ID AS M_ID
FROM EMPLOYEES
WHERE EXISTS (
SELECT 1
FROM MANAGERS
WHERE MANAGERID = ID
)
Or better yet, use an alias for the tables:
SELECT e.NAME, e.ID AS M_ID
FROM EMPLOYEES AS e
WHERE EXISTS (
SELECT 1
FROM MANAGERS AS m
WHERE m.MANAGERID = e.ID
)
Column aliases can only be used in the ORDER BY
, GROUP BY
and HAVING
clauses. Standard SQL doesn't allow you to refer to a column alias in a WHERE
clause. This restriction is imposed because when the WHERE
code is executed, the column value may not yet be determined.
By the way - using a subselect for the given problem might not be the best solution. Even though in this simple case I'd assume that the MySQL query optimizer can find a simple way in the execution plan.