Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在where条件下具有所有必填值的Oracle sql查询_Sql_Oracle - Fatal编程技术网

在where条件下具有所有必填值的Oracle sql查询

在where条件下具有所有必填值的Oracle sql查询,sql,oracle,Sql,Oracle,我想在ORACLE中编写一个select查询,仅当where条件中给出的所有值都存在时才会返回记录。例如 select * from emp where empid in (7521,7566,7698) 现在我想编写这个查询,以便它只在所有3个empid都存在时返回值。它将与empid=7521、empid=7566和empid=7698类似。如果任何一个值不存在,则此查询不应获取任何行。再次以嵌套select方式运行同一查询,并对其记录进行计数 select * from emp whe

我想在ORACLE中编写一个select查询,仅当where条件中给出的所有值都存在时才会返回记录。例如

select * from emp where empid in (7521,7566,7698)

现在我想编写这个查询,以便它只在所有3个empid都存在时返回值。它将与empid=7521、empid=7566和empid=7698类似。如果任何一个值不存在,则此查询不应获取任何行。

再次以嵌套select方式运行同一查询,并对其记录进行计数

select * from emp 
where empid in (7521, 7566, 7698)
and 3 = (select count(*) from emp where empid in (7521, 7566, 7698))
或者,对原始结果使用分析函数并检查:

select * from (
  select emp.*, count(*) over() as cnt
  from emp where empid in (7521, 7566, 7698)
)
where cnt = 3

Lukas版本的扩展,您只需编写一次ID:

with emp_ids as (
   select 7521 as eid from dual
   union all 
   select 7566 from dual
   union all
   select 7698 from dual
)
select * 
from (
  select emp.*, 
         count(*) over() as cnt
  from emp_new emp
  where empid in (select eid from emp_ids)
)
where cnt = (select count(*) from emp_ids);

在这些情况下,我总是错过其他DBMS中可用的标准行构造函数,在这里我可以简单地编写
值(7521)、(7566)、(7698)
,以生成具有所需值的虚拟表,而无需使用双…

,但我的In条件是动态的,它不是固定为3的only@user1017936:我想,那么,您可以动态渲染它吗?您将相关ID存储在哪里?实际上,它用于某些搜索条件。是的,我可以计数,但有一个问题,用户将以逗号分隔的值在字符串中输入值,因此我无法在查询中使用。@user1017936:我不确定我是否理解。。。但我想这是一个新的无关问题,值得一个新的问题。就查询而言,我给了您两个有效的选项。现在这里有一些复杂性,即我们从emp_ID检索的eid也有一些父项,因此我的计数将在那里增加。这不是您在问题或您对卢卡斯答案的评论中所描述的(我不确定“有一些父项”是什么意思)很好的解决方案。我还没有想到CTE用于构建特别的内存表。事实上,很遗憾,Oracle中甚至没有任何默认的
VARRAY
类型…@user1017936:如果该解决方案工作正常,也许您应该接受它。查看如何将值列表转换为可以在in语句中使用的内容。从这里开始,为了确保您拥有所有这些,我建议您使用分析函数来计算带有“无”名称的“马”所建议的值,并确保与列表中的项目数相匹配,可以使用length(id_list)-length(replace(id_list,,)+1找到这些项目