我有 2 个表:Person 和 Item。我刚刚在 Item 表中创建了一个名为 item_lab_fk 的新列。
我需要从 Person 表中选择 person_lab_fk 值并将其放置在 Item 表的 item_lab_fk 列中。
我尝试运行以下一段 sql 查询,但它无法正常工作。
如果有人能告诉我下面代码中的错误所在,我将不胜感激:
INSERT INTO item (`item_lab_fk`)
SELECT person.person_lab_fk
FROM person WHERE person.per_oid = item.item_created_by
请注意,item.item_created_by
字段是 Person 表中的 person_id
。
最佳答案
这是手工输入且未经测试的,但应该会让您走上正确的道路
UPDATE Item
SET Item.item_lab_fk = Person.person_lab_fk
FROM Item INNER JOIN Person ON Person.per_old = Item.item_created_by
关于mysql - 从另一个表插入表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6015694/