rownum是oracle才有的写法,rownum在oracle中可以用于取第一条数据,或者批量写数据时限定批量写的数量等

mysql取第一条数据写法

  SELECT * FROM t order by id LIMIT 1;

oracle取第一条数据写法

  SELECT * FROM t where rownum =1 order by id;

ok,上面是mysql和oracle取第一条数据的写法对比,不过这只是rownum的一种用法,rownum还可以用于批量写数据

往t表批量写一万条数据:

  insert into t(id,date) select sys_guid(),sysdate from dual connect by rownum<=10000;

oracle原版写法:

select * from (select id,name from t) where rownum <![CDATA[<=]]> to_number(num);

mysql改写后的SQL:

SELECT 
  * 
FROM
  (SELECT 
    tb.*,
    @rownum := @rownum + 1 AS rownum 
  FROM
    (SELECT 
      id,
      NAME 
    FROM
      t) tb,
    (SELECT 
      @rownum := 0) r) AS t 
WHERE rownum <= CAST(num AS SIGNED INTEGER) ;

版权声明:本文为mzq123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mzq123/p/11470607.html