Oracle Cannot Update TOP N Issue, 请专家解答
2015-02-11 10:24 钟铧若岩 阅读(425) 评论(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里找到办法,请数据库专家指导,感谢为盼!