“每个派生表必须有自己的别名”,即使我给它们别名

时间:2013-08-13 05:44:37

标签: mysql sql

我有这个查询,它从前一行的列('next_line')中选择数据,从最近一行('raw_line')的不同列中选择数据。

SELECT CONCAT((SELECT `next_line` FROM `lines` ORDER BY id DESC LIMIT 1 OFFSET 1), (SELECT     
`raw_line` FROM `lines` ORDER BY id DESC LIMIT 1))

此查询完美无缺。但我想要做的是将此输出放入最近一行*('composed_line')*中的不同列。但是当我这样做时:

UPDATE `lines` set `composed_line`=CONCAT((SELECT `next_line` FROM `lines` ORDER BY id DESC LIMIT 
1 OFFSET 1), (SELECT `raw_line` FROM `lines` ORDER BY id DESC LIMIT 1)) ORDER BY id DESC LIMIT 1;

我收到此错误:

#1093 - You can't specify target table 'lines' for update in FROM clause 

所以我尝试了这个:

UPDATE `lines` set `composed_line`=CONCAT((SELECT `next_line` FROM (SELECT * FROM `lines` ORDER 
BY id DESC LIMIT 1 OFFSET 1) AS `alias`, (SELECT `raw_line` FROM (SELECT * FROM `lines` ORDER BY 
id DESC LIMIT 1)) AS `alias2` ORDER BY id DESC LIMIT 1

我收到了这个错误:

 #1248 - Every derived table must have its own alias 

我看不出我做错了什么 - 'alias''alias2'不算作派生表别名吗?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您的内部选择也需要别名:

UPDATE `lines` set `composed_line`= 
CONCAT((SELECT `next_line` FROM (SELECT * FROM `lines` ORDER 
BY id DESC LIMIT 1 OFFSET 1) AS `alias`, 
(SELECT `raw_line` FROM (SELECT * FROM `lines` ORDER BY 
id DESC LIMIT 1) AS `alias3` ) AS `alias2` ORDER BY id DESC LIMIT 1

另请参阅:every derived table must have its own alias