【发布时间】:2015-05-15 15:37:55
【问题描述】:
有没有办法编写具有以下功能的查询,添加 where 子句作为条件方式,
select e.emp_id, emp.admin_user from employees e
如果 emp.admin != 'Y'
然后
使用 where 子句运行查询
否则
没有 where 子句的查询运行?
【问题讨论】:
-
一个简单的解决方案是添加一个 if 语句来检查条件,然后有两个 select,一个带有 where 子句,一个不依赖 if 语句的结果。
-
您可以在 where 子句中使用 CASE 表达式。看我的回答。
-
select * from employees WHERE project_id in (CASE WHEN employees.ADMIN_USER != 'Y' then THEN (select distinct project_id from PROJECTS_ACCESSIBLE_TO_USER where emp_id = 1002332) ELSE ename END)
标签: oracle oracle11g case conditional-statements where