MySQL的语法错误附近 “从(子查询) '表'”

问题描述:

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

我不明白为什么我收到此错误:MySQL的语法错误附近 “从(子查询) '表'”

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

我已经检查了手册(MySQL subquery in FROM clause),我看不出任何例子和我的小陈述之间的区别。

表名/别名必须被用反引号或没有

select * 
from (select * from table1) table1; 

我想你想反引号而不是向前引号括起来:

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

正向引号指定一个字符串常量。后面的引号分隔一个名字。

当你有一个表是子查询时,你需要声明一个名字。

select * from (select * from table1) as x 
+0

不完全正确。 OP正在这样做,但使用单引号使别名成为字符串,而不是别名/字段/表名称。 – 2013-04-24 21:51:29

除了不把引号的别名,我相信你也需要反引号周围的子查询“表”,因为它是在MySQL中的保留字(假设你没有真正命名表“表”):

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