Oralce in IN查询中的clouse

时间:2017-09-26 07:58:27

标签: sql oracle

Emp_id, sal, age, exp
1        2   21   9
2        2   21   9
3        2   22   9
4        2   33   9
5        2   33   10


select sum(sal) from emp_table where age in (21,22,33,...);

Need to add some condition 
for age 21 No condition
for age 22 No condition
for age 33 exp > 10;

必需输出:sum(sal):8 如何在oracle

中的单个查询中实现此目的

3 个答案:

答案 0 :(得分:2)

response

试试这个。

答案 1 :(得分:1)

您的病情可以改写成以下形式:

select sum(sal) 
from emp_table 
where age = 21 or age = 22 or age = 33;

因此,如果您需要为每个age表达式添加任何后续条件,则将其添加到括号中并使用AND,例如像这样

select sum(sal) 
from emp_table 
where age = 21 or 
      age = 22 or 
      (age = 33 AND exp > 10);

编辑:如果你想保持IN,那么我会做以下事情:

SELECT sum(sal) 
FROM emp_table 
WHERE age IN (21, 22) OR
      (age = 33 AND exp > 10);

答案 2 :(得分:0)

您需要将IN语句与以下行中的内容分开:

select sum(sal) from emp_table where (age = 21 AND some_condition) OR age = 22 OR (age = 33 AND exp>10)