0

我有这个查询,它从前一行的一个列('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'算作派生表别名吗?

非常感谢任何帮助!

4

1 回答 1

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

另请参阅:每个派生表都必须有自己的别名

于 2013-08-13T05:50:23.940 回答