Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the error "Every derived table must have its own alias" in MySQL?

I am running this query on MySQL

SELECT ID FROM (     SELECT ID, msisdn     FROM (         SELECT * FROM TT2     ) ); 

and it is giving this error:

Every derived table must have its own alias.

What's causing this error?

like image 855
silverkid Avatar asked Dec 11 '09 15:12

silverkid


People also ask

How do you set an alias for a derived table?

The short answer is you need to give your subqueries an alias in your SELECT statement. Add an alias after the closing bracket of the FROM clause subquery. In other SQL vendors, this is not required, but MySQL requires you to alias your subqueries.

What is error 1248 in MySQL?

mySQL error: #1248 - Every derived table must have its own alias.

What is derived table in MySQL?

A derived table is an expression that generates a table within the scope of a query FROM clause. For example, a subquery in a SELECT statement FROM clause is a derived table: SELECT ... FROM (subquery) [AS] tbl_name ...


2 Answers

Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever), which can the be used to refer to it in the rest of the outer query.

SELECT ID FROM (     SELECT ID, msisdn FROM (         SELECT * FROM TT2     ) AS T ) AS T 

In your case, of course, the entire query could be replaced with:

SELECT ID FROM TT2 
like image 108
Paul Avatar answered Oct 07 '22 18:10

Paul


I think it's asking you to do this:

SELECT ID FROM (SELECT ID,              msisdn        FROM (SELECT * FROM TT2) as myalias      ) as anotheralias; 

But why would you write this query in the first place?

like image 32
hometoast Avatar answered Oct 07 '22 20:10

hometoast