我有这个查询,它从前一行的列之一(“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' 不算作派生表别名吗?
非常感谢任何帮助!
最佳答案
您的内部选择还需要一个别名:
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
关于mysql - "Every derived table must have its own alias"即使我给了他们别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201485/