search

Home  >  Q&A  >  body text

MySQL error: "Each derived table must have its own alias" What does it mean?

I'm running this query on MySQL

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

It gives this error:

Each derived table must have its own alias.

What causes this error?

P粉218361972P粉218361972497 days ago841

reply all(2)I'll reply

  • P粉545218185

    P粉5452181852023-10-11 18:53:28

    I think it requires you to do this:

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

    But why are you writing this query in the first place?

    reply
    0
  • P粉729436537

    P粉7294365372023-10-11 17:33:00

    Each derived table (also called a subquery) does have to have an alias. ie. Each query within parentheses must specify an alias (ASwhat) that can be used to reference it in the rest of the outer query.

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

    Of course, in your case the entire query can be replaced with:

    SELECT ID FROM TT2

    reply
    0
  • Cancelreply