[英]How do I write an Update SQL to copy multiple records from one table to update the corresponding field on another table?
I would like to select all the records in one field that are grouped together by a foreign key, and update the corresponding field in another table that is grouped by a foreign key. 我想在一个字段中选择一个由外键分组在一起的所有记录,并更新在另一个表中由外键分组的对应字段。 The foreign keys between the two tables are different and are not related to each other.
两个表之间的外键是不同的,并且彼此不相关。
Ideally, I'd like the tables to go from looking like State A to State B 理想情况下,我希望表格从状态A变为状态B
State A 状态A
tbl_Unlisted tbl_Listed
ListID ListDate ListID ListDate
43 04/01/2018 64
43 04/02/2018 64
43 04/03/2018 64
43 04/04/2018 64
State B 状态B
tbl_Unlisted tbl_Listed
ListID ListDate ListID ListDate
43 04/01/2018 64 04/01/2018
43 04/02/2018 64 04/02/2018
43 04/03/2018 64 04/03/2018
43 04/04/2018 64 04/04/2018
Here is my stab at the Update SQL: 这是我在Update SQL时遇到的问题:
UPDATE tbl_Listed
SET ListDate = tbl_Unlisted.ListDate
FROM tbl_Unlisted
WHERE tbl_List.ListID = 64
AND tbl_Unlisted.ListID = 43;
tbl_Listed is the table that I want to update tbl_Listed是我要更新的表
tbl_Unlisted is the table that contains the existing records tbl_Unlisted是包含现有记录的表
ListDate is the field that I want to be copied from tbl_Unlisted and updated into tbl_Listed ListDate是我要从tbl_Unlisted复制并更新为tbl_Listed的字段
ListID is the field that contains the foreign key for tbl_Listed and tbl_Unlisted ListID是包含tbl_Listed和tbl_Unlisted外键的字段
However, the Update SQL throws a syntax error when I try to execute it. 但是,当我尝试执行Update SQL时会引发语法错误。
Any idea on what I should change? 有什么想法我应该改变吗? Thank you in advance!
先感谢您!
EDIT - Updated from comments
编辑-从评论更新
I should clarify that there is another field on each table that will be unique to each record so that row #1 = A, row #2 = B, row #3 = C, row #4 = D 我要澄清的是,每个表上还有一个字段对每个记录都是唯一的,因此行#1 = A,行#2 = B,行#3 = C,行#4 = D
You should leverage SQL Query Design mode and create this query easily 您应该利用SQL查询设计模式并轻松创建此查询
If you;re going to use VBA - I'd suggest parameterized query for the different ListIDs 如果您要使用VBA,则建议对不同的ListID使用参数化查询
This will work if your FieldName for the matching rows is called "ROW" 如果匹配行的FieldName称为“ ROW”,则此方法有效
UPDATE tbl_Listed
INNER JOIN tbl_Unlisted
ON tbl_Listed.Row = tbl_Unlisted.Row
SET tbl_Listed.ListDate = [tbl_Listed].[ListDate]
WHERE (((tbl_Listed.ListDate) Is Null)
AND ((tbl_Listed.ListID)=64)
AND ((tbl_Unlisted.ListID)=43));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.