Monday, December 18, 2006

MySQL's workaround for Oracle's ROWNUM

This is a common question that come across the minds of lot of developers especially in the initial stages of their migration from oracle to MySQL.

MySQL's equivalent to Oracle's rownum is limit.
Limit takes two numeric arguments, the first argument is the offset of first row to return (offset of the first row in the results starts from 0 and not 1) and the second argument takes the maximum number of rows to return from the specified offset.

e.g: where rownum between 10 and 20 <---> limit 10,20

One other common use of rownum in oracle to copy the table structure.
e.g: create table mytable_copy as (select * from mytable where rownum <0);

The equivalent in MySQL is
create table mytable_copy as (select * from mytable limit 0);

For more information, please refer to this link on MySQL website.

Hope this helps. Please contact me in case of any queries.

No comments: