简体   繁体   English

Oracle SQL查询中的条件

[英]Condition in oracle sql query

I have a query like 我有一个查询

 select * from emp where emp.id in(1,2,3) and emp.sal>10000

I need to add a logic to the check emp.sal>10000 only if design.type='Labor', I tried like the below but not working 仅当design.type ='Labor'时,我才需要向emp.sal> 10000添加逻辑,我尝试如下所示但不起作用

select * from emp,design where emp.id in(1,2,3) and case when design.type='Labor' then emp.sal>10000 

Can we implement this ? 我们可以实现吗?

Simple: 简单:

select * from emp where emp.id in(1,2,3) and (design.type<>'Labor' OR emp.sal>10000)

If design.type='Labor' then emp.sal must be greater than 10000 for the condition to be true. 如果design.type ='Labor',则emp.sal必须大于10000,条件为true。

This should work for you... 这应该为您工作...

SELECT * FROM emp 
WHERE emp.id IN (1,2,3) 
   AND ((design.type = 'Labor' AND emp.sal>10000) 
        OR (design.type <> 'Labor'))

The simplification of the OR condition would be.. OR条件的简化将是..

SELECT * FROM emp 
WHERE emp.id IN (1,2,3) 
   AND (design.type <> 'Labor' OR emp.sal > 10000)

You want to query rows that have a salary higher than 10000 only when the type is labor for completion's sake, we will also state that for different type s we don't care about the salary. 您想查询有高于10000薪水只有当类型为行labor完成的缘故,我们也都注明了不同type小号,我们不关心工资。 If we put this formally, we'll get: 如果我们正式提出这一点,我们将得到:

(type = 'Labor' and salary > 10000) OR (type != 'Labor')

This boolean expression can of course be simplified - we're looking for rows that either have a type different than Labor or a salary higher than 10000: 当然,可以简化此布尔表达式-我们正在寻找行类型不同于Labor或薪水高于10000的行:

(salary > 10000) OR (type != 'Labor')

So if we put it all together: 因此,如果我们将所有内容放在一起:

SELECT *
FROM   emp 
WHERE  (emp.id IN (1,2,3)) AND (type != 'Labor' OR sal > 10000)
  Try this.
  select * from 
  (
    select * from emp where id in (1,2,3) and design.type='Labor'
  )a
  where a.sal>10000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM