【问题标题】:Not in to not exists不存在不存在
【发布时间】:2019-05-08 15:21:15
【问题描述】:

我有一个“不存在”,我试图在我的选择的 where 子句中变成一个“不存在”

我尝试切换最低限度但没有填充任何内容(它确实执行了

where  [a] not in (
select 
    [a] 
from table
group by [a]
having count(*) > 1) -- Ignores Records with duplicate data

【问题讨论】:

    标签: sql sql-server exists


    【解决方案1】:

    你可以这样做:

    where not exists (select 1
                      from table t2
                      where ?.a = t2.a
                      group by t2.a
                      having count(*) > 1
                     )
    

    不过,这通常会在查询的表中寻找对相同值的另一个引用。如果是这样,请避免聚合:

    where not exists (select 1
                      from table t2
                      where ?.a = t2.a and ?.id <> t2.id
                     )
    

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 2015-09-28
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2022-01-24
      相关资源
      最近更新 更多