【问题标题】:Selecting based on condition having multiple options根据具有多个选项的条件进行选择
【发布时间】:2012-08-10 10:57:57
【问题描述】:

我对在 where 子句之后或在提到 table_name 之后的任何地方使用聚合函数感到完全困惑

http://viditkothari.co.in/post/27045365558/sql-commands-1 上发布的 EMP 表

查询信息:
显示所有sal等于deptno 30的任何emp的emp

建议查询:

select * 
  from employee_4521 
 where sal having (select sal 
                     from employee_4521 
                    where deptno = 30);

返回以下错误:

第 1 行出现错误:
ORA-00920: 无效的关系运算符

在有子句的“h”下标有星号

【问题讨论】:

  • 如果您需要提出第二个问题,请随时创建第二个线程。如果您在一个线程中提出多个不相关的问题,事情就会变得复杂。

标签: sql oracle oracle11g aggregate


【解决方案1】:

似乎没有任何理由在这里使用聚合函数。只需使用INEXISTS

select * 
  from employee_4521 
 where sal in (select sal 
                 from employee_4521 
                where deptno=30);

select * 
  from employee_4521 a
 where exists( select 1
                 from employee_4521 b
                 where b.deptno = 30
                   and a.sal = b.sal );

【讨论】:

  • 非常感谢您的解决方案,但我不明白 select 子查询后第二个解决方案中 1 的用途。跨度>
  • 我的一个朋友建议:select * from employee_4521 where sal=any(select sal from employee_4521 where deptno=30);这是正确的吗?
  • @viditkothari - 使用ANY 将是非常规的,但与查询的IN 版本基本相同。 EXISTS 查询中的 1 是无关紧要的——你可以在那里选择任何你想要的东西。 EXISTS 子句只检查子查询是否至少返回一行。你可以在那里选择任何你喜欢的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多