如何根据oracle中另一个表中的行的值选择列

时间:2015-06-24 22:26:07

标签: sql oracle

我有两张表如下。我想根据第一个表

中的行的值从第二个表中选择一列
    table1                       table2
id     pos1   pos2         id1      id2      id3      position
41a1   A1T1   V1          1122     41a1      99a1     A1T1V1
1133   A1T1   V2          1133     41a2      99a2     A1T1V2
99a3   A1T1   V3          1144     41a3      99a3     A1T1V3
41a2   A1T1   V4
41a3   A1T1   V5

如何在oracle中执行select查询

select t1.id,(t2.id1 or t2.id2 or t2.id3) from t1,t2 where t1.pos1||t1.pos2= t2.position; 

t2.id1 t2.id2或t2.id3基于t1.id类似于下面的if循环的值:

  if t1.pos1 ='A1T1' and substr(t2.position,1,4)='A1T1' and substr(t1.id,1,2)='41' 
  then 
      t2.id2 
  elseif 
     substr(t1,id,1,2)='99' 
  then  
      t2.id3 
  else 
     t2.id1 

1 个答案:

答案 0 :(得分:0)

听起来你想要一个case陈述

select t1.id,
       (case when t1.pos1 ='A1T1' 
              and substr(t2.position,1,4)='A1T1' 
              and substr(t1.id,1,2)='41' 
             then t2.id2
             when substr(t1,id,1,2)='99' 
             then t2.id3 
             else t2.id1 
         end)
   ...