CREATE TABLE temp_a1(a1 NUMBER(10) NULL,a2 NUMBER(10) NULL,a3 VARCHAR2(100) NULL);
CREATE TABLE temp_b1(a2 NUMBER(10) NULL,b2 VARCHAR2(100) NULL);
INSERT INTO temp_a1 VALUES(1,10,'');
INSERT INTO temp_a1 VALUES(2,11,'');
INSERT INTO temp_b1 VALUES(10,'1200');
INSERT INTO temp_b1 VALUES(10,'120');
INSERT INTO temp_b1 VALUES(10,'10');
INSERT INTO temp_b1 VALUES(11,'1220');
UPDATE temp_a1 a SET a.a3=(SELECT b2 FROM ( SELECT b2 FROM temp_b1 b WHERE b.b2=a.a2 ORDER BY length(b2) DESC) WHERE ROWNUM=1)
标识符无效
这样不行啊,求换种方法实现
--这样又没办法排序了,
UPDATE temp_a1 a SET a.a3=(SELECT b2 FROM temp_b1 b WHERE b.a2=a.a2 AND rownum=1 )
求帮助,如何实现才能UPDATE 按照length(b2)排序后取b2的的最长的那个值
------解决方案--------------------
- SQL code
--试下这个看能否达到你的效果update temp_a1 aset a3=(select b2 from temp_b1 b where a.a2=b.a2 and rownum=1and length(b2)=(select max(length(b2)) from temp_b1 bx where b.a2=bx.a2));
------解决方案--------------------
UPDATE temp_a1 a SET a.a3=(SELECT b2
(select c.*,row_number() over(partition by c.a2 order by length(c.b2) desc) rn
FROM temp_b1 c
)b WHERE b.a2=a.a2 AND b.rn=1 )
------解决方案--------------------
取b2的的最长的那个值,这个和你的排序是没有影响的,可以直接用max 在b2中取值即可:
- SQL code
update temp_a1 a set a.a3 = (select max(b.b2) b2 from temp_b1 b where b.b2 = a.a2)