关于Rack()自增长的一则双表关联更新

A表(tb_abc):


A B
1 aa
0200
2 bb 0300
3 cc 0500
4 dd 1800
5 ee 2200
6 ff 3300

B表(tb_abcc):


A
B
1 aa
(0201)
2 aa (0202)
3 bb (0301)
4 bb (0302)
5 bb (0303)
6 cc (0501)

括号里是预期值

规则: 通过B表的a字段匹配A表的a字段,并读取A表的b字段,按照该值依次增数写入B表b字段

实现:

update
  tb_abcc c
set
  c.b =
  (select
    tmp.str
  from
    (select
      b.rowid rd,
      b.a,
      substr(a.b, 1, 2) || lpad(
        (
          rank () over (partition by b.a
            order by b.rowid)
        ),
        2,
        0
      ) str
    from
      tb_abc a,
      tb_abcc b
    where a.a = b.a) tmp
  where c.rowid = tmp.rd)
where exists
  (select
    'x'
  from
    (select
      b.rowid rd,
      b.a,
      substr(a.b, 1, 2) || lpad(
        (
          rank () over (partition by b.a
            order by b.rowid)
        ),
        2,
        0
      ) str
    from
      tb_abc a,
      tb_abcc b
    where a.a = b.a) tmp
  where c.rowid = tmp.rd);

6 rows updated

select * from tb_abcc;

A    B

---- ------

aa   0201

aa   0202

bb   0301

bb   0302

bb   0303

cc   0501


6 rows selected