1

我正在尝试从三个表中选择数据。我的逻辑应该是这样的:

基本上从由表 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

任何指针?

4

3 回答 3

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
于 2012-08-31T14:49:42.213 回答
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
于 2012-08-31T14:49:44.437 回答
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
于 2012-08-31T14:54:08.830 回答