[英]How can i insert data into a column of a table (out of 2 tables) such that user id matches in both of them?
[英]How to insert a column that matches id from both tables?
我有 2 个表,用户和等级。 用户看起来像这样:
ID | Name | Area
1 | John | Hub
和这样的成绩:
ID | ID_from | ID_for | G1 | G2 | Area
1 | 23 | 22 | 9 | 8 | NULL
我想将 Users 表中的区域插入到 grades 表中,其中users.id = grades.ID_for
。
我已经尝试过
INSERT INTO grades
SELECT area FROM users
where ID_for = users.id;
但我得到“#1054 - Champ 'ID_for' inconnu dans where 子句”。
需要为区域表提供的值的数量是 6(它们是ID | ID_from | ID_for | G1 | G2 | Area
)。 但您只提供 3 个(ID | 姓名 | 区域)
您需要 map 从源表到目标表的所有列。
而且,看起来您正在尝试更新区域而不是插入。 如果是这样的话
update grades
set Area = (select area from users where users.id = grades.ID_for)
可能会做的工作。 如果您打算仅更新区域的 null 值,则
update grades
set Area = (select area from users where users.id = grades.ID_for)
where area is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.