【发布时间】:2017-01-10 04:04:09
【问题描述】:
我有 3 张桌子:
- A(t_ref 整数,l_date 日期),
- B(t_ref 整数,client_ref 整数),
- C(t_id 整数,client_ref 整数)
和参数(client_ref_ integer)。
我需要从表 A 中选择具有条件的数据:
1)如果表B中有行与client_ref相连(如果查询
select b.t_ref from B b where b.client_ref = client_ref_
返回任何数据),我的查询将如下所示:
select max(l_date) from A where t_ref in (select b.t_ref from B b where b.client_ref = client_ref_)
2) 如果上面的查询返回任何数据,我的查询将如下所示:
select max(l_date) from A where t_ref in (select c.t_id from C c where c.client_ref = client_ref_)
现在我写了一个 PLSQL 函数:
select max(aa.l_date) into l_date from A aa where aa.t_ref in (select bb.t_ref from B bb where bb.client_ref = client_ref_);
if l_date is null then
select max(aa.l_date) into l_date from A aa where t_ref in (select t_id from C c where c.client_ref = client_ref_);
end if;
return l_date;
它有效,但这不是一个好主意,因为我调用了表 A 2 次。是否可以避免第二次调用,并在一个查询中执行此操作?
【问题讨论】:
-
在您的函数中,第一个选择将失败。您已将表别名为 aa 并在选择查询中使用单个 a。 max(a.l_date) 到 l_date
-
对不起,这是 typinq 错误。但这不是主要问题。
标签: sql oracle plsql outer-join