简体   繁体   中英

How to join a substring of a column (in one table) with the full value of another column (in another table)

So I currently have a table with the following columns and entries:

ROW_ID        CODE           VAL
  1            US             50
  2            CAN            15
  3            MEX            12

And I have another column with the following columns and entries:

ROW_ID       CODE_METADATA         REGION
  1           US|451223123           8
  2          CAN|123123123           7
  3          MEX|41028               3

How would I be able to join the two tables on the CODE and CODE_METADATA columns?(respectively). I've tried the following but it didn't work:

select t2.nvl(substr(code_metadata, 0, instr(code_metadata, '|')-1), code_metadata) as CODE
 from table1 t1
 join table2 t2 on t1.code = t2.CODE;

Thanks in advance!

您可以加入条件:

substr(t2.CODE_METADATA,1,instr(t2.CODE_METADATA,'|')-1) = t1.CODE

您也可以使用LIKE加入:

t2.CODE_METADATA LIKE t1.CODE || '|%'

Try this: substr(t2.CODE_METADATA,1,length(t1.CODE)) = t1.CODE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM