Search code examples
mysqlsyntax-errorin-subquery

Mysql syntax error when using an alias for a subquery


If i don't put aliases to the subqueries i get the error "Error Code: 1248\n Every derived table must have its own alias". If i put aliases i get a syntax error near the alias ([...]syntax to use near ' bb[...]).

(The following code has been overly-simplified for clarity and pivacy reasons)

SELECT MAX(id) FROM
    (SELECT id FROM
      stoc
    WHERE id_gest IN (SELECT ida FROM nom_gest) bb
      ) aa

I tried with the keyword 'AS' and without, i tried putting the alias in between quotes, i tried diferent paranthesis configurations and nothing.

I also tried just running it with just ONE subquery: it Works without an alias. but if i put an alias i get a syntax error near the alias([...]syntax to use near ' bb[...]).


Solution

  • This is a sub query not a derived table so you don't need the alias so remove that bb.

    WHERE id_gest IN (SELECT ida FROM nom_gest) bb
    

    When you put a sub query in the FROM section it's a derived table.