假设我在代码中的不同位置使用了两个Oracle SQL查询:
select *
from employees
where manager_id IS NULL
and etc etc etc...
Run Code Online (Sandbox Code Playgroud)
和
select *
from employees
where manager_id IS NOT NULL
and etc etc etc...
Run Code Online (Sandbox Code Playgroud)
两个查询之间的唯一区别是manager_id在第一个查询中为NULL,在第二个查询中为NOT NULL.
有没有办法把它写成一个通用语句?是否可以在参数中传入NULL或NOT NULL?像这样的东西:
select *
from employees
where manager_id = ?
and etc etc etc...
Run Code Online (Sandbox Code Playgroud)
我知道上面的例子不起作用(我知道为什么).我的问题更像是管理两个99%相似的独立SQL字符串是否有更优雅的解决方案?
您需要使用更复杂的逻辑.也许:
where ( (? = 'not null' and manager_id is not null) or
(? = 'null' and manager_id is null)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |