3 つのテーブルからデータを選択しようとしています。私のロジックは次のようになります。
基本的に、表Aの列によって決まる2つの表のいずれかから選択します。
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
ポインタはありますか?
3 つのテーブルからデータを選択しようとしています。私のロジックは次のようになります。
基本的に、表Aの列によって決まる2つの表のいずれかから選択します。
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
ポインタはありますか?
使用する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
頭に浮かぶのは、各テーブルから結果を取得するために結合された 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
テーブル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