繁体   English   中英

在MS ACCESS / VBA上的SQL NOT IN查询

[英]SQL NOT IN Query on MS ACCESS/VBA

我有以下课程表,具有不同的提取日期。

ID | Course | ExtractDate
10 | 100000 | 2017-02-28
10 | 100001 | 2017-01-31
10 | 100002 | 2016-12-31
10 | 100003 | 2016-11-30

我需要执行以下SQL脚本以仅保留最近3个月的记录,并因此删除课程代码为10003的记录。

Delete from [Course] where [ExtractDate] not in (select distinct top 3 [ExtractDate] from [Course] order by [ExtractDate] desc

好像VBA或MS Access不支持“不在”功能,有人对此有任何解决方法吗?

谢谢。

NOT IN与目标为空的左联接相同(因此没有联接)

所以...

from [Course]
where [ExtractDate] not in (
   select distinct top 3 [ExtractDate] 
   from [Course] 
   order by [ExtractDate] desc
)

是相同的

from [Course]
left join (
   select distinct top 3 [ExtractDate] 
   from [Course] 
   order by [ExtractDate] desc
) as x on [ExtractDate] = x.[ExtractDate]
where x.[ExtractDate] is null

但是,我不能断言您的任何代码是正确的-只是这两个在SQL中是相同的。

如果你想超过三个月较旧的记录:

Delete from [Course]
    where [ExtractDate] < dateadd("m", -3, date());

无需基于集合的操作。

暂无
暂无

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

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