Mysql Error Every derived table must have its own alias Solution

  • 2021-12-11 19:20:11
  • OfStack

mysql Errors when performing multi-table queries:


[SQL] SELECT * from 
(
select e.account from employee e
UNION
SELECT u.account from `user` u
UNION
SELECT a.account from agent a
)
[Err] 1248 - Every derived table must have its own alias

This sentence means that each derived table must have its own alias

This error usually occurs in multi-table queries or subqueries, because in nested queries, the result of a subquery is queried as a derived table to the upper level 1, so the result of a subquery must have an alias.

In the above example, modify the query statement by 1:


SELECT * from 
(
select e.account from employee e
UNION
SELECT u.account from `user` u
UNION
SELECT a.account from agent a
)as total

As shown above, adding a sentence as total after the subquery is equivalent to aliasing the result set derived table of the subquery as total, and the problem is solved.


Related articles: