Skip to content Skip to sidebar Skip to footer

"inverse" Limit?

I'm using MySQL to store financial stuff, and using the data to build, among other things, registers of all the transactions for each account. For performance reasons - and to kee

Solution 1:

The documentation says:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

  • Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

  • Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT*FROM tbl LIMIT 5,10;  # Retrieve rows6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT*FROM tbl LIMIT 95,18446744073709551615;

Next time, please use the documentation as your first port of call.

Solution 2:

You can hack it this way:

selectsel.*
from
(
SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r, YourTableOrYourSubSelect t
) selwhererownum > 40

It's kinda like having Oracle's rownum in MySQL.

Post a Comment for ""inverse" Limit?"