[英]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 3
值three
。
谢谢!
这看起来像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.