你不能。但是您可以连接两个表,然后在select
子句中选择您想要的值:
SELECT A.id, A.flag,
(case when coalesce(A.flag, 0) = 0 then b.fname else c.fname end) as fname,
(case when coalesce(A.flag, 0) = 0 then B.lname else c.lname end) as lname
FROM Table1 A left outer join
Table2 B
on B.id = A.id left outer join
Table3 C
on c.id = A.id
如果 Table2 或 Table3 中有任何 id 的多行,这将产生额外的行。
作为一种通常效率较低的替代方法,您还可以执行以下操作:
select a.id, a.flag,
MAX(case when coalesce(A.flag, 0) = 0 and which = 'b' or
coalesce(A.flag, 0) <> 0 and which = 'c'
then b.fname
end) as fname,
MAX(case when coalesce(A.flag, 0) = 0 and which = 'b' or
coalesce(A.flag, 0) <> 0 and which = 'c'
then b.lname
end) as lname
from table1 A left outer join
((select b.*, 'b' as which from table2 b)
union all
(select c.*, 'c' as which from table3 c)
) b
group by a.id, a.flag
这group by
将消除不需要的重复项。