我有一张桌子:
sou_id sou-name rool_upid roll_upname
1 AA null null
2 BB null null
3 CC null null
1 DD null null
2 EE null null
5 CC null null
我需要首先使用更新语句来获取 - 如果在roll_upid
和roll_upname
即。 更新上面的表后应该有这样的价值
sou_id sou-name rool_upid roll_upname
1 AA 1 DD
2 BB 2 EE
3 CC 3 CC
1 DD 1 DD
2 EE 2 EE
5 CC 5 CC
现在AGAIN检查并更新gor ----如果roll_upname
相同而且roll_upid
是多次占用最多。
我需要另一个更新语句。
因此,第二次更新后的决赛桌应该是这样的:
sou_id sou-name rool_upid roll_upname
1 AA 1 DD
2 BB 2 EE
3 CC 5 CC
1 DD 1 DD
2 EE 2 EE
5 CC 5 CC
请尽快帮助我,我需要它 提前谢谢
答案 0 :(得分:0)
CREATE TABLE TABLE1 (SOU_ID int, SOU_NAME varchar2(2), ROOL_UPID varchar2(4), ROLL_UPNAME varchar2(4));
INSERT ALL INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (1, 'AA', NULL, NULL) INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (2, 'BB', NULL, NULL) INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (3, 'CC', NULL, NULL) INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (1, 'DD', NULL, NULL) INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (2, 'EE', NULL, NULL) INTO Table1 (SOU_ID, SOU_NAME, ROOL_UPID, ROLL_UPNAME) VALUES (5, 'CC', NULL, NULL) SELECT * FROM dual;
6 rows affected
update table1 t set t.roll_upname = (select max(table1.sou_name) from table1 where table1.sou_id = t.sou_id)
6 rows affected
update table1 t set t.rool_upid = (select max(table1.sou_id) from table1 where table1.roll_upname = t.roll_upname)
6 rows affected
select * from table1
SOU_ID | SOU_NAME | ROOL_UPID | ROLL_UPNAME -----: | :------- | :-------- | :---------- 1 | AA | 1 | DD 2 | BB | 2 | EE 3 | CC | 5 | CC 1 | DD | 1 | DD 2 | EE | 2 | EE 5 | CC | 5 | CC
dbfiddle here