[英]Oracle Conditional where clause
is there any way to write query with following functionality, add where clause as a conditional way, 有没有办法用以下功能编写查询,添加where子句作为条件方式,
select e.emp_id, emp.admin_user from employees e
if emp.admin != 'Y' 如果emp.admin!='Y'
then 然后
query run with where clause 查询运行where子句
else 其他
query run without where clause ? 查询运行没有where子句?
Using a CASE expression in the WHERE clause should do the trick. 在WHERE子句中使用CASE表达式应该可以解决问题。 When you say you don't need the where clause if condition is not met, then all you want is a condition like WHERE 1 = 1
, ie when condition is not met then return all rows. 当你说如果不满足条件你不需要where子句时,你想要的只是WHERE 1 = 1
条件,即当条件不满足时返回所有行。 So, you need to make the not met condition as always TRUE . 因此,您需要使不满足条件始终为TRUE 。
For example, 例如,
I have an employee table, 我有一张员工桌,
SQL> SELECT empno, ename, deptno
2 FROM emp;
EMPNO ENAME DEPTNO
---------- ---------- ----------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected.
SQL>
I want to select the employee details, if department is 20 then use the where clause else return all the employee details, but filter the department which meets the where condition. 我想选择员工详细信息,如果部门是20然后使用where子句,则返回所有员工详细信息,但过滤满足where条件的部门。
SQL> SELECT empno, ename, deptno
2 FROM emp
3 WHERE ename =
4 CASE
5 WHEN deptno = 20
6 THEN 'SCOTT'
7 ELSE ename
8 END
9 /
EMPNO ENAME DEPTNO
---------- ---------- ----------
7499 ALLEN 30
7521 WARD 30
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7900 JAMES 30
7934 MILLER 10
10 rows selected.
SQL>
So, for department 20, the filter is applied by where clause, and I get only the row for ename SCOTT, for others it returns all the rows. 因此,对于部门20,过滤器由where子句应用,我只获取ename SCOTT的行,而其他行则返回所有行。
To keep it simple I would go for union clause in this case, so you can have your where clause as complex as you need. 为了简单起见,我会在这种情况下使用union子句,因此您可以根据需要使用where子句。 I tried to guess your table structure from above comment, let's see this example: 我尝试从上面的评论中猜测你的表结构,让我们看看这个例子:
SQL> create table employees (emp_id number, admin_user number, project_id number);
Table created. 表创建。
SQL> create table project_accessible_to_user (emp_id number, project_id number);
Table created. 表创建。
Now make simple union all of two queries one with where condition anoother without it 现在简单地将所有两个查询联合起来,在没有它的情况下使用其他条件
SQL> select * from employees e where e.admin_user!='Y' and project_id in
(select project_id from project_accessible_to_user where emp_id=e.emp_id)
union all
select * from employees e where (e.admin_user is null or
e.admin_user='Y');
UNION ALL is better from performance point of view as UNION because it means that it is not checking for intersect values so if there are any it will return duplicates. 从性能的角度看UNION ALL作为UNION更好,因为它意味着它不检查交叉值,所以如果有的话它会返回重复。 However in this case it is filtered already by condition on admin_user, so these duplicates will not occure. 但是在这种情况下,它已在admin_user上按条件过滤,因此不会发生这些重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.