简体   繁体   English

MS Access SQL UPDATE 使用多个表

[英]MS Access SQL UPDATE using multiple tables

I am trying to update Table 1 using values from Table 2 and Table 3. There is no relation field between Table 1 and (Tables 2 and 3) so I am using a conditional WHERE Statement.我正在尝试使用表 2 和表 3 中的值更新表 1。表 1 和(表 2 和表 3)之间没有关系字段,所以我使用的是条件 WHERE 语句。 I only want to pull CF from table 3 when the table3.TOS is 7 and pull the region portion from table2.stateID when table1.zip is between or equal to the start/end zip codes in table 2. Table 2 and 3 relate on a stateID field.我只想在 table3.TOS 为 7 时从表 3 中提取 CF,并在 table1.zip 介于或等于表 2 中的开始/结束 zip 代码时从 table2.stateID 中提取区域部分。表 2 和表 3 相关一个 stateID 字段。

This code seems to work but when run Access says updating 5880 rows despite Table 1 only having 1471 rows.这段代码似乎可以工作,但是当运行 Access 时说更新 5880 行,尽管表 1 只有 1471 行。 I am wondering what in my code is causing this number to be so inflated and when I run it, the tables look good with correct numbers.我想知道我的代码中是什么导致这个数字如此膨胀,当我运行它时,表格看起来不错,数字正确。 Again THIS CODE RUNS AS-IS but tells me it is updating way more rows than expected.再次,此代码按原样运行,但告诉我它正在更新比预期更多的行。

    UPDATE table1, table2, table3
    SET table1.region = '00' + right(table2.[stateID],2),
    table1.cf = table3.cf,
    table1.eff_date = '2020-03-01'
    WHERE (table3.[TOS] = '7' AND table2.[stateID] = table3.[stateID])
    AND (table1.[zip] = table2.[zip_start] 
    OR table1.[zip] = table2.[zip_end] 
    OR (table1.[zip] BETWEEN table2.[zip_start] AND table2.[zip_end]));

You can simplify the WHERE clause to:您可以将WHERE子句简化为:

WHERE table3.[TOS] = '7' AND
      table2.[stateID] = table3.[stateID] AND
      table1.[zip] BETWEEN table2.[zip_start] AND table2.[zip_end];

I wonder if MS Access is reporting multiple updates on the same row.我想知道 MS Access 是否在同一行报告多个更新。

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

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