brisebom
brisebom

Reputation: 123

MySQL syntax error near "from (subquery) 'table'"

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.

Upvotes: 1

Views: 1852

Answers (4)

tjmw
tjmw

Reputation: 309

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;

Upvotes: 0

Val
Val

Reputation: 413

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

select * from (select * from table1) as x

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269753

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.

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

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

select *
from ( select * from table1 ) table1;

Upvotes: 3

Related Questions