1
select *
from ( select * from table ) 'table1';

I cannot see why I am getting this error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''table1'' at line 2

I have checked the manual (MySQL subquery in FROM clause) and I can't see any difference between the examples and my little statement.

4 Answers 4

3

Table names/aliases must either be surrounded with backticks or nothing

select *
from ( select * from table1 ) table1;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you want back quotes rather than forward quotes:

select *
from ( select * from table ) `table1`;

Forward quotes specify a string constant. Back quotes delimit a name.

Comments

0

when you have a subquery that is a table, you need to declare it with a name.

select * from (select * from table1) as x

1 Comment

not quite true. OP is doing it, but using single quotes which makes the alias a string, not an alias/field/table name.
0

As well as not putting quotes around the alias, I believe you also need backticks around "table" in the subquery because it's a reserved word in MySQL (assuming you did actually name your table "table"):

select * from ( select * from `table` ) table1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.