繁体   English   中英

Microsoft Access更新查询-从另一个表插入主键

[英]Microsoft Access Update Query - Insert Primary Key from Another Table

好吧,因为我是MS Access的新手,所以被困了几天。

假设我有三个表:

  • OrderTable(包含字段:otID作为自动编号的主键,custID,Ref)。
  • CustomerTable(包含字段:custID作为自动编号的主键,custName,custPhone和custEmail)。
  • OP-tMain(包含字段:custName,custPhone,custEmail和Ref)。

所有数据都存储在Excel中,我将使用OperationTable在Access中处理和存储数据。

我可以执行UPDATE QUERY来更新CustomerTable。 更新CustomerTable之后,我需要更新订单表。 我面临的问题是在OrderTable中,我省略了custName,custPhone和custEmail,而仅使用custID(我认为这是正确的方法)。 任何人都可以帮助我如何从OP-tMain更新/插入数据并从CustomerTable中提取相应的cID?

编辑。 在此处上传的示例文件==> https://drive.google.com/file/d/1fK3IDEd54Jfr6c-U2s5Bnm4TeK2TQZnM/view

考虑使用联接查询进行追加和更新:

追加查询

INSERT INTO OrderTable (CustId, Ref)
SELECT c.CustID, o.Ref
FROM CustomerTable c
INNER JOIN [OP-tMain] o
ON c.custName = o.custName

更新查询以使用每个客户多个引用条目的相关计数匹配来更新引用

UPDATE (OrderTable m
INNER JOIN CustomerTable c ON c.CustID = m.CustID)
INNER JOIN [OP-tMain] o ON c.custName = o.custName
SET m.Ref = o.Ref
WHERE DCOUNT("*", "OrderTable", "otID <=" & m.otID & " AND custID =" & m.custID) = 
      DCOUNT("*", "[OP-tMain]", "ID <=" & o.ID & " AND custName ='" & o.custName & "'")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM