像这样名为categories
的表
IdRows | CategoriesName | UpperLevelName
--------------------------------------------
1 | SomeName |
例如,想要插入第二行,看起来像这样
IdRows | CategoriesName | UpperLevelName
--------------------------------------------
2 | SomeNameNext | SomeName
像这样查询
INSERT INTO categories (
CategoriesName,
UpperLevelName
)
VALUES (
?,
(SELECT CategoriesName FROM categories WHERE IdRows = ?)
)
我的意思是UpperLevelName
希望在CategoriesName
IdRows = 1
但是得到错误You can't specify target table 'categories' for update in FROM clause
理解错误在这里(SELECT CategoriesName FROM categories WHERE IdRows = ?)
什么是正确的代码?
答案 0 :(得分:0)
insert ... select
语法不包含values
关键字。而且你需要构建一个临时表,因为在MySQL中你无法更新你正在选择的表
INSERT INTO categories (CategoriesName, UpperLevelName)
SELECT * from
(
select ?, CategoriesName FROM categories WHERE IdRows = ?
) tmp