ms access查询以选择不在表中的项

j13ufse2  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(231)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

11个月前关门了。
改进这个问题
windows,ms access 2016。
我有一个表,其中通常有编号为n&n+1的项目组

Items
  100     
  101    
  200   
  201
  300
  301
  400
  401

某些项目可能丢失:

Items
  100     
  200   
  201
  301
  400   
  401

在这里,我想找到没有相应+/-1差异的项目。在上表中,我想找到项目100(缺少101),301(缺少300)。我如何编写一个sql语句来选择它们?

omtl5h9j

omtl5h9j1#

你可以用 mod 操作员:

select t.*
from t
where (t.items mod 100 = 0 and
       not exists (select 1 from t as t2 where t2.items = t.items + 1)
      ) or
      (t.items mod 100 = 1 and
       not exists (select 1 from t as t2 where t2.items = t.items - 1)
      )

或者,可以使用聚合:

select min(items)
from t
where items mod 100 in (0, 1)
group by items / 100
having min(items) = max(items)

相关问题