首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL查询以检查值是否不存在

SQL查询以检查值是否不存在
EN

Stack Overflow用户
提问于 2021-12-07 19:10:26
回答 1查看 72关注 0票数 0

首先,我真的不知道如何提出我的问题。

数据库里有不同的公司。

我想知道哪些公司没有“分析师”档案。

以下是我的疑问:

代码语言:javascript
运行
复制
select
t.name as "name"
t.pre as "first name"
t.id as "# account"
t.profile as "Profile"
b.cod_miss as "Mission"
b.df_missn as "End date"


from sr.v t 
inner join od.e_lds on t.niu_ld = b.niu_ld
where b.cod_miss = 'APPROV'
and t.profile = 'Analyst'

这个查询为我的数据库中的每个公司提供了所有分析师。但我希望所有没有分析师的公司。我该怎么做?我试着用‘和t.profile <>’分析师“,”但很明显这不是很好.

编辑

我尝试了接受的答案,但我注意到它只是返回给我每个不是分析师的人。

假设有一家X公司有3名员工。其中一个是分析师。我不希望那家公司在我的结果上有所出格。但如果Y公司有2名员工,而且没有一名是“分析师”,那么我希望这家公司能在结果中得出结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-07 19:14:16

如果我没听错的话,那就是not exists。就像这样:

代码语言:javascript
运行
复制
select *
from sr.v 
where not exists (select null 
                  from od.e_lds b
                  where b.niu_ld = t.niu_ld
                    and t.profile = 'Analyst'
                 );

应用于查询:

代码语言:javascript
运行
复制
select
  t.name as "name"
  t.pre as "first name"
  t.id as "# account"
  t.profile as "Profile"
  b.cod_miss as "Mission"
  b.df_missn as "End date"
from sr.v t inner join od.e_lds b on t.niu_ld = b.niu_ld
where b.cod_miss = 'APPROV'
  --
  and not exists (select null 
                  from od.e_lds c
                  where c.niu_ld = t.niu_ld
                    and t.profile = 'Analyst'
                 );

编辑#2,带有一些示例数据

这是一个例子,展示了你试图用文字解释的东西(不过,如果你贴出样本数据会更好)。正如你所看到的,宝马的一名员工是分析师,而SAP中没有人是>>,因此,SAP被退回。

代码语言:javascript
运行
复制
SQL> with test (company, ename, profile) as
  2    (select 'BMW', 'Scott', 'Analyst' from dual union all
  3     select 'BMW', 'King' , 'Manager' from dual union all
  4     select 'BMW', 'Mike' , 'Clerk'   from dual union all
  5     --
  6     select 'SAP', 'John' , 'Clerk'   from dual union all
  7     select 'SAP', 'Fred' , 'Manager' from dual
  8    )
  9  select a.company, a.ename, a.profile
 10  from test a
 11  where not exists (select null
 12                    from test b
 13                    where b.company = a.company
 14                      and b.profile = 'Analyst');

COMPANY    ENAME PROFILE
---------- ----- -------
SAP        Fred  Manager
SAP        John  Clerk

SQL>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70265731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档