Fork me on GitHub

mysql类似oracle rownum写法

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) ;

posted @ 2019-09-05 23:57  smileNicky  阅读(6330)  评论(0编辑  收藏  举报