我试图创建一个追加查询,看起来我的语法错了。 当我运行追加查询时,我不希望再次追加旧记录,而只是新记录。我将包含我的SQL语句。这也是在访问中运行。
INSERT INTO Table1 ( District, Location, PricePerTon )
SELECT TestFormWithDeficiency1.District,
TestFormWithDeficiency1.Location,
TestFormWithDeficiency1.PricePerTon
FROM TestFormWithDeficiency1
WHERE (((TestFormWithDeficiency1.RecordCopy)=Yes))
Where Not Exist (
Select Location
Form Table1
Where Table1.Location=TestFormWithDeficiency1.Location);
答案 0 :(得分:1)
如果where子句中存在多个条件,请使用AND
而不是第二个where
INSERT INTO Table1 ( District, Location, PricePerTon )
SELECT TestFormWithDeficiency1.District,
TestFormWithDeficiency1.Location,
TestFormWithDeficiency1.PricePerTon
FROM TestFormWithDeficiency1
WHERE TestFormWithDeficiency1.RecordCopy=Yes
AND NOT EXISTS (Select Location -- AND instead of second WHERE
FROM Table1 -- FROM instead of FORM
WHERE Table1.Location=TestFormWithDeficiency1.Location);
答案 1 :(得分:0)
而不是使用EXISTS
加入表,以便返回TestFormWithDeficiency1
中的所有记录,只返回Table1
中的匹配记录。然后,您可以检查哪个Table1.Location
为空。
INSERT INTO Table1 ( District, Location, PricePerTon )
SELECT TestFormWithDeficiency1.District
, TestFormWithDeficiency1.Location
, TestFormWithDeficiency1.PricePerTon
FROM TestFormWithDeficiency1 LEFT JOIN Table1 ON TestFormWithDeficiency1.Location = Table1.Location
WHERE RecordCopy=Yes AND Table1.Location IS NULL
注意:在WHERE
子句中,我没有为RecordCopy
指定表格。这假定Table1
中不存在该字段。因为它只出现在一个表中,所以不需要使用表名完全限定。
在大型表上,这应该比使用子查询运行得快一点(或者是相关查询?)