我知道您不应该将计算值存储在数据库中,但在这种情况下,结构已经给出,我必须处理它。
我有两张桌子:
Table1
与字段(即customer, product, price, count
)
Table2
与字段(即customer, product, description
)
我现在需要用表 2 中匹配条目的数量更新表 1 中的字段“计数”。这两个表需要通过“客户”和“产品”连接。
我的想法是这样的:
UPDATE Table1 SET Table1.count =
(SELECT COUNT(Table2.customer)
FROM Table2
WHERE Table2.customer = Table1.customer AND Table2.product = Table1.product)
WHERE Table1.count IS NULL
但这给出了一个错误:
操作必须是可更新的查询。
我正在搜索这边和网络,建议使用 DCount 函数,所以我重写了我的代码来做到这一点:
UPDATE Table1
SET Tabl1.count = DCount( "*", "Table2", "Table2.product = "& Table1.product AND "Table2.customer = "& Table1.customer)
WHERE Table1.count IS NULL
不幸的是,这总是返回所有存在于Table2
. 因此,如果我在Table2
DCount 值中有 100 个条目 = 100,而不是特定条目的匹配条目数量Table1
(客户和产品相同)。
有人可以指出我在该语句中缺少的内容,以便我可以使用匹配条目的数量更新“计数”列Table2
。