![](/img/trans.png)
[英]SQL - specifying a condition in COUNT / returning a value from the same field more than once in one SELECT
[英]sql select condition more than another condition from same column
我有 Emp_Period 和 Emp_Salaries 表。 我需要选择工资高于不同条件的另一个工资的数据。
选择语句
select *
from Employee
where Emp_Period >96
and Emp_Salary < (select Emp_Salary
from Employee
where Emp_Period <24)
小于或大于运算符无法检查多条记录。 但在这里您可以执行像in ()
高级运算符
演示
select *
from Employee
where Emp_Period >96
and Emp_Salary in (select Emp_Salary
from Employee
where Emp_Period <24)
啊谢谢,我需要'any'来回答这个问题。 这是我的代码
select *
from Employee
where Emp_Period >96
and Emp_Salary <any (select Emp_Salary
from Employee
where Emp_Period <24 )
你需要小心。 我倾向于对这个逻辑使用聚合:
e.Emp_Salary > (select max(e2.Emp_Salary)
from Employee e2
where e2.Emp_Period < 24
)
(注意表别名和限定列名的使用。)
如果没有员工满足条件,子查询将返回NULL
值。 SQL 使用ALL
具有类似的机制:
e.Emp_Salary > all (select e2.Emp_Salary
from Employee e2
where e2.Emp_Period < 24
)
ANY
在这里不合适。
此版本与聚合版本略有不同。 如果没有员工与子查询中的条件匹配,则聚合版本为false
。 如果没有员工符合条件,则ALL
版本为true
。 目前尚不清楚问题要求的是哪个版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.