zoukankan      html  css  js  c++  java
  • Oracle Cannot Update TOP N Issue, 请专家解答

    大家好

    上周写了匿名方法一文,很多读者,很高兴,相信我们已经从大伙的回复中,对.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里找到办法,请数据库专家指导,感谢为盼!

     

     

     

  • 相关阅读:
    openvas漏洞扫描
    MSF基础应用
    MS11-050安全漏洞
    MS08_067漏洞渗透攻击实践
    Adobe阅读器渗透攻击
    详解CentOS7安装配置vsftp搭建FTP
    zabbix英文切换成中文
    代理抛出异常 : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: 你的主机名: 你的主机名
    安装zabbix客户端
    Docker报错:“WARNING: IPv4 forwarding is disabled. Networking will not work.”解决。
  • 原文地址:https://www.cnblogs.com/xhu218/p/4285453.html
Copyright © 2011-2022 走看看