赞
踩
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 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) ;
<Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。