Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting all records using SQL LIMIT and OFFSET query

I wonder if there is a way to accomplish:

SELECT * FROM table 

by using LIMIT and OFFSET like so:

SELECT * FROM table LIMIT all OFFSET 0 

Can I write SQL statement using LIMIT and OFFSET but still getting ALL result?

* of course I can use an IF statement but I rather avoid it if possible

like image 542
Lior Elrom Avatar asked Apr 11 '13 14:04

Lior Elrom


People also ask

How offset and limit works in SQL?

The limit option allows you to limit the number of rows returned from a query, while offset allows you to omit a specified number of rows before the beginning of the result set. Using both limit and offset skips both rows as well as limit the rows returned.

How do I SELECT all records in SQL?

SELECT * FROM <TableName>; This SQL query will select all columns and all rows from the table. For example: SELECT * FROM [Person].

How do you limit the number of records to return from a query?

The SQL LIMIT clause constrains the number of rows returned by a SELECT statement. For Microsoft databases like SQL Server or MSAccess, you can use the SELECT TOP statement to limit your results, which is Microsoft's proprietary equivalent to the SELECT LIMIT statement.

How can I get Limit records in SQL?

SELECT TOP, LIMIT and ROWNUMThe LIMIT , SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL Server uses SELECT TOP . MySQL uses LIMIT , and Oracle uses ROWNUM .


2 Answers

This may not be the best way to do it, but its the first that comes to mind...

SELECT * FROM myTable LIMIT 0,1000000

Replace 1000000 with some adequately large number that you know will always be larger than the total number of records in the table.

like image 20
Nate S. Avatar answered Oct 05 '22 07:10

Nate S.


From the MySQL documentation:

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;

So getting all rows might look as follows:

SELECT * FROM tbl LIMIT 0,18446744073709551615; 
like image 179
sroes Avatar answered Oct 05 '22 05:10

sroes