[英]mysql query to insert the value of a column with respect to another column in same table
[英]Mysql Insert ColumnName is (SELECT value form another column of the same table)
將表命名為這樣的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
要在IdRows = 1
插入CategoriesName
但是出現錯誤You can't specify target table 'categories' for update in FROM clause
據了解錯誤在這里(SELECT CategoriesName FROM categories WHERE IdRows = ?)
什么是正確的代碼?
insert ... select
語法不包含values
關鍵字。 而且您需要建立一個臨時表,因為在MySQL中您無法更新要從中選擇的表
INSERT INTO categories (CategoriesName, UpperLevelName)
SELECT * from
(
select ?, CategoriesName FROM categories WHERE IdRows = ?
) tmp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.