简体   繁体   English

如何编写更新SQL来从一个表中复制多个记录以更新另一表上的相应字段?

[英]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.

相关问题 SQL用另一个表中的记录更新一个表的记录? - SQL to Update records of one table with records from another table? 如何用另一个表中的字段更新一个表中的字段? (SQL) - How can I update a field in one table with a field from another table? (SQL) 如何编写存储过程以从不同架构的另一个表更新一个架构的一个表? - How do I write stored procedures to update one table of one schema from another table of a different schema? 从一个表列到另一表的SQL Update记录 - SQL Update records from one table column using another table 在SQL中,如何基于另一个字段的值将值从一个表复制到另一个表? - In SQL How do I copy values from one table to another based on another field's value? UPDATE查询可基于辅助字段从一个表复制到另一个表 - UPDATE query to copy from one table to another based on a secondary field 如何在SQL中将行从一个表复制到另一个表? - How do I copy rows from one table to another in SQL? 如何从一个表复制或更新到另一表 - How to copy or update from one table to another table 我如何在sql中限制,如果选择了一个表行,则仅显示另一表中的相应项目? - How do i limit in sql, that if one table row is selected, only corresponding items from another table show? 从另一个多记录表更新多记录表 - Update multiple records table from another multiple records table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM