如何使用更新查询更新同一列

问题描述 投票:0回答:1

我正在尝试根据 CIF 更新标志列。我想不通。请帮助进行更新/合并查询

sql oracle oracle11g
1个回答
0
投票

merge
,正如你所说,可能是一种选择。

之前:

SQL> select * From test;

       CIF FLAG
---------- ----
       120 SF
       120 NA
       165 SF
       165
       158 MF
       158

6 rows selected.

合并:

SQL> merge into test a
  2    using (select b.cif, max(b.flag) flag
  3           from test b
  4           group by b.cif
  5          ) x
  6    on (a.cif = x.cif)
  7    when matched then update set
  8      a.flag = x.flag
  9      where a.flag is null;

2 rows merged.

之后:

SQL> select * From test;

       CIF FLAG
---------- ----
       120 SF
       120 NA
       165 SF
       165 SF
       158 MF
       158 MF

6 rows selected.

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.