gpt4 book ai didi

sql - 使用带有 NOT EXISTS 的 INNER JOIN Access INSERT 语句会给出错误的结果

转载 作者:行者123 更新时间:2023-12-01 13:00:19 25 4
gpt4 key购买 nike

我有一个 Access .mdb 数据库。有两个结构相同的表:Prices 和 tmpPrices。每个表都有三列:[截至日期标准](日期/时间)、价格( double )、CUSIP(文本,255 个字符)。 tmpPrices 包含要添加到 Prices 的新记录。我有两个 SQL 查询从 tmpPrices 更新价格,一个使用 WHERE 连接,另一个使用 INNER JOIN。

以下版本 A 正常工作:

INSERT INTO [Prices] SELECT * FROM [tmpPrices] WHERE NOT EXISTS 
(SELECT * from [Prices]
WHERE ([Prices].[As of date std] = [tmpPrices].[As of date std])
AND ([Prices].CUSIP = [tmpPrices].CUSIP));

而这个版本 B 不起作用:
INSERT INTO [Prices] SELECT * FROM [tmpPrices] WHERE NOT EXISTS
(SELECT [Prices].* FROM [Prices] INNER JOIN [tmpPrices] ON
([Prices].[As of Date std] = [tmpPrices].[As of Date std])
AND ([Prices].CUSIP = [tmpPrices].CUSIP));

两个子查询都给出相同的结果:来自 tmpPrices 中已经存在于 Prices 中的记录子集。

如果不是我必须更新其他表,使用版本 A 更新另一个表需要大约 45 分钟,但使用版本 B 只需要一小部分时间,并且它似乎可以正常工作,使用版本 A 不会有太大影响。所以我想了解这里发生了什么。

最佳答案

正如@devsh 所建议的那样,LEFT JOIN 绝对是可行的方法,但您不需要使用子查询。我在 Access 的查询设计器中构建了它,它在 Access 2003 中与您描述的表一起使用。我确实重命名了 [As of date std] 以消除空格。

INSERT INTO Prices ( As_of_Date_std, Price, CUSIP)
SELECT t.As_of_Date_std, t.Price, t.CUSIP
FROM
tmpPrices AS t
LEFT JOIN Prices AS p
ON (t.As_of_Date_std = p.As_of_Date_std) AND (t.CUSIP = p.CUSIP)
WHERE (((p.As_of_Date_std) Is Null));

关于sql - 使用带有 NOT EXISTS 的 INNER JOIN Access INSERT 语句会给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6497635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com