0
假设我们有以下四个表:如何避免关联CASE中的子查询?
create table table_x(x number, title varchar2(100));
create table table_y(y number, x number, title varchar2(100));
create table table_z(z number, y number, x number, title varchar2(100));
create table table_main(z number, y number, x number);
它需要根据此规则在table_main
每一行获取title
:
select
case
when z is not null
then (select title from table_z where x = tm.x and y = tm.y and z = tm.z)
when y is not null
then (select title from table_y where x = tm.x and y = tm.y)
when x is not null
then (select title from table_x where x = tm.x)
end as title
from table_main tm;
我在寻找生产相同的结果更可读的解决方案。
有没有一种方法来重构查询替换case when
语句与表连接或以某种方式?
这些都是标量子查询。你可以通过使用三个左连接和一个合并()来避免它们() – joop
@joop看起来像我们有同样的想法;-) –