Sql 具有All运算符的相关子查询
运行此查询时遇到问题:Sql 具有All运算符的相关子查询,sql,Sql,运行此查询时遇到问题: select empname,empnum from emp a where a.sal> All (select sal from emp b where b.hiredate<a.hiredate) 错误消息: 不支持具有相关表达式的所有子查询 该查询应确保提供的所有员工的工资高于他们之后雇用的任何人 当此查询工作时: select ename,empnum from emp a where not exists (select sal from e
select empname,empnum
from emp a
where a.sal> All (select sal from emp b where b.hiredate<a.hiredate)
错误消息:
不支持具有相关表达式的所有子查询
该查询应确保提供的所有员工的工资高于他们之后雇用的任何人
当此查询工作时:
select ename,empnum
from emp a
where not exists (select sal from emp b where b.hiredate<a.hiredate and b.sal>a.sal)
您可以将其替换为几乎相等的查询的聚合:
select empname, empnum
from emp e
where e.sal > (select max(e2.sal)
from emp e2
where e2.hiredate < e.hiredate
);
唯一的区别是,您的查询将返回雇用日期最早的员工,而实际情况并非如此。您可以将其调整为:
select empname, empnum
from emp e
where e.sal > (select coalesce(max(e2.sal), e.sal - 1)
from emp e2
where e2.hiredate < e.hiredate
);
请注意,使用有意义的表格别名而不是任意字母。用您正在使用的数据库标记您的问题。因此,为了澄清问题,您希望从答案中学到什么?感谢您的帮助!但我仍然想理解为什么“ALL”操作符在我提出的解决方案中不起作用。我在网上搜索,没有遇到任何关于将“>ALL”添加到相关子查询的说法,因为这是有问题的。这是什么错误消息?@ShadyAbs。错误信息非常清楚:您使用的数据库不支持您尝试执行的操作。