代码改变世界

Oracle Cannot Update TOP N Issue, 请专家解答

  钟铧若岩  阅读(426)  评论(0编辑  收藏  举报

大家好

上周写了匿名方法一文,很多读者,很高兴,相信我们已经从大伙的回复中,对.NET又有了更深刻的认识.

好,现在说主题,各类数据库都有相应更新本表top n的方案.现在我一一举例

首先看表结构如下:

数据库以及表创建命令初始化数据库语句

1 CREATE TABLE student(
2 id varchar(256)   NOT NULL,
3 name varchar(256)   NULL,
4 age int NULL)

 

复制代码
 1 insert into student values('001','wfg',20);
 2 insert into student values('002','lxx',21);
 3 insert into student values('003','wly',3);
 4 insert into student values('004','jcj',60);
 5 insert into student values('005','wss',60);
 6 insert into student values('006','xsm',60);
 7 insert into student values('007','lcf',60);
 8 insert into student values('008','wjy',35);
 9 insert into student values('009','hyf',35);
10 insert into student values('010','lwl',12);
复制代码

 表格结构如下: 

ID Name Age
001 wfg 20
002 lxx 21
003 wly 3
004 jcj 60
005 wss 60
006 xsm 60
007 lcf 60
008 wjy 35
009 hyf 35
010 hwl 12

需求如下:按姓名顺序后,更新前5个的年龄为100岁,如何办到.

首先说下MSSQL 

1 --方案1失败
2 update top(5) student set age = 100  order by name;
3 --方案2ok
4 update  student set age = 100 where id in (select top 5 id from student order by name );
5 select top(5) * from student order by name;

很成功,干得漂亮!

接下来我们看看MYSQL呢,拭目以待...

1 update student set age = 100  order by name limit 5;
2 select * from student order by name limit 5;

 Good news, MySql干得不错,

接下来我们再看看oracle呢?

1 --方式1失败
2 update student set age = 100 where  rownum <5 order by name ;
3 commit;
4 --方式2失败
5 update student set age = 100 where id in (select id from student  where rownum<5 order by name);
6 commit;
7 select * from student where rownum<5 order by name;

 目前为此,还没有在oracle里找到办法,请数据库专家指导,感谢为盼!

 

 

 

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示