繁体   English   中英

在 oracle-sql 中映射列

[英]Mapping columns in oracle-sql

桌子:

column 1           column 2           column 3
2                  two                3
5                  five               8
3                  three              10
8                  eight              11
12                 one                15

我想创建一个新的列column 4 ,如下所示:

 column 1           column 2           column 3       column 4
2                  two                3               three
5                  five               8               eight
3                  three              10              
8                  eight              11
12                 one                15

我想 map column 3 column 1 ,如果有匹配, column 4取第column 2列的值。

示例:第 3 column 3中的值3存在于column 1中,因此column 4将采用对应的column 3three

谢谢!

这看起来像left join

select t.*, tt.column2 as column4
from table1 t left join
     table1 tt
     on t.column3 = tt.column1;

编辑:

如果要设置值,可以使用update

update table1 t
    set column4 = (select tt.column2 from table1 tt where t.column3 = tt.column1)
    where exists (select 1 from table1 tt where t.column3 = tt.column1);

然而,这似乎很愚蠢。 您可以使用显式join或隐藏视图中的逻辑轻松获取表中的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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