簡體   English   中英

如何基於Oracle中另一個表中的行的值選擇列

[英]how to select a column based on the value of a row from another table in oracle

我有2張桌子,如下所示。 我想根據第一張表中的行的值從第二張表中選擇一列

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 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基於類似於以下if循環的if t1.id值:

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

聽起來像是您要一份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)
   ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM