我想更新表格最后两行的1个字段。所以我需要一个子查询。 两个sql都可以 - 我如何组合这两个SQL命令?
选择命令(有效,最后2行):
SELECT * FROM (select * from mytable WHERE id='62741' ORDER BY lfdnr DESC) mytable2 WHERE rownum <= 2;
结果:
LFDNR ID M2
361782 62741 8,5
361774 62741 8,6
更新(?,存在,合并?)
UPDATE mytable set m2='8,4' WHERE EXISTS (select * from mytable WHERE id='62741' and rownum <=2 ORDER BY lfdnr DESC);
结果:
Fehlerbericht -
SQL-Fehler:ORA-00907:缺少右括号
00907. 00000 - “缺少右括号”
*原因:
*操作:
谢谢你的帮助! 迈克尔
答案 0 :(得分:1)
您可以使用rowid
伪列:
update mytable set m2 = '8, 4'
where rowid in (select rowid
from (
select rowid, row_number() over (order by lfdnr desc) rn
from mytable where id = '62741')
where rn <= 2 )
测试:
create table mytable (id varchar2(5), lfdnr number(5), m2 varchar2(10));
insert into mytable values ('62705', 1, 'abc');
insert into mytable values ('62741', 2, 'xyz');
insert into mytable values ('62741', 3, 'qwe');
insert into mytable values ('62741', 4, 'rty');
ID LFDNR M2
----- ------ ----------
62705 1 abc
62741 2 xyz
62741 3 8, 4
62741 4 8, 4