我正在尝试从三个表中选择数据。我的逻辑应该是这样的:
基本上从表A中的列确定的两个表中的任何一个表中选择。
Select a.orderid ,
if (a.ordertable= b) then b.order_info
else
c.order_info
where
a.order_id = b.order_id or a.order_id = c.order_id
任何指针?
答案 0 :(得分:5)
使用CASE
SELECT a.orderid,
CASE
WHEN a.ordertable = b.? THEN b.order_info
ELSE c.order_info
END
FROM sparkles
WHERE a.order_id = b.order_id OR a.order_id = c.order_id
答案 1 :(得分:2)
我想到的是两个子查询,联合起来得到每个表的结果:
select *
from ((select b.order_info
from b join
a
on a.order_id = b.order_id and
a.ordertable = 'b'
)
union all
(select c.order_info
from c join
a
on a.order_id = c.order_id and
c.ordertable = 'c'
)
) t
答案 2 :(得分:1)
假设表b或c中的行可能存在,也可能不存在,我认为你需要这个:
select a.orderid,
case
when a.ordertable = 'b' then b.order_info
else c.order_info
end as order_info
from a
left
join b
on a.orderid = b.orderid
left
join c
on a.orderid = c.orderid