MYSQL start at nth result (opposite of LIMIT)

1.4k views Asked by At

Is there a SQL command for starting at a certain amount of results? Example:

SELECT * FROM table WHERE ID=1 BEGIN AT 100
3

There are 3 answers

1
Rashid On BEST ANSWER

SELECT * FROM table WHERE ID=1 LIMIT 100,500;

result will show 500 value from 100

1
Leandro Bardelli On

From: http://dev.mysql.com/doc/refman/5.0/en/select.html

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;

In your case:

SELECT * FROM table WHERE ID=1 BEGIN AT 99,18446744073709551615

More information:

For prepared statements, you can use placeholders (supported as of MySQL version 5.0.7). The following statements will return one row from the tbl table:

SET @a=1;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?';
EXECUTE STMT USING @a;

The following statements will return the second to sixth row from the tbl table:

SET @skip=1; SET @numrows=5;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
EXECUTE STMT USING @skip, @numrows;
0
Edper On

Try to create a temporary Row Number then use that as criteria for your starting point which is in your case 100, like:

 SELECT * FROM (
      SELECT (@RowNum := @RowNum + 1) as Row, tbl.* FROM tbl, 
        (SELECT @RowNum:=0) ctr
       ) t
 WHERE Row >= 100

See Demo