你不能。但是,你可以加入這兩個表,然後選擇您的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
這將產生額外的行是否有表2表3或者多行的任何標識。
只是作爲一個替代方案,這通常是效率較低,你也可以這樣做:
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
將消除不必要的重複。
你會需要一些動態SQL這裏。 – Kermit 2013-02-08 19:36:17