9

I would like to limit the amount of rows I fetch in MySQL. Can you show me how?

ex:

  • 1st query I would like to retrieve only the first 10,000 records
  • 2nd query I would like to retrieve only records from 10,000 - 20,000

etc

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
chcne
  • 91
  • 1
  • 1
  • 2
  • 1
    possible duplicate of [PHP/MySQL Pagination](http://stackoverflow.com/questions/3095474/php-mysql-pagination) – OMG Ponies Aug 03 '10 at 18:28

7 Answers7

11

The term you're looking for is "pagination." Unfortunately, this is done differently depending on the SQL engine.

For MS SQL Server, see this Stack Overflow question.

Since you mentioned MySQL, it's actually quite simple:

SELECT [columns] FROM [a table] LIMIT 10000
SELECT [columns] FROM [a table] LIMIT 10000 OFFSET 10000

The first statement fetches results 1-10,000, and the second statement fetches results 10,001-20,000.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
5

I think the following queries will give you the desired result

SELECT * FROM PERSON_TBL LIMIT 0, 10000

@ 1st query I would like to retrieve only the first 10,000 records

SELECT * FROM PERSON_TBL LIMIT 10000,10000

@ 2nd query I would like to retrieve only records from 10,000 - 20,000

Alessandro.Vegna
  • 1,262
  • 10
  • 19
Dora
  • 292
  • 3
  • 15
3
select top x * from table in SQL Server

select * from table where ROWNUM < x in Oracle

select * from table limit x in MySQL
Jay
  • 1,286
  • 2
  • 11
  • 20
2

MySQL and PostgreSQL support OFFSET that is usually used with a LIMIT clause.

SELECT column FROM table
LIMIT 10000

SELECT column FROM table
LIMIT 10000 OFFSET 10000
miku
  • 181,842
  • 47
  • 306
  • 310
1

in mysql you do as follows

SELECT * FROM PERSON_TBL LIMIT 0, 1000 

SELECT * FROM PERSON_TBL LIMIT 1000, 1000 

Query 1 will fetch first 1000 records,

Query 2 will fetch next 1000 records

Syntax for limits clause

LIMITS OFFSET, ROWCOUNT

Where ROWCOUNT give number of row to fetch

OFFSET gives from which row to fetch more info here

indianwebdevil
  • 4,879
  • 7
  • 37
  • 51
0

in MySQL :

SELECT * FROM `your_table` LIMIT 0, 10000 

This will display the first 10000 results from the database.

 SELECT * FROM `your_table` LIMIT 10000, 20000 

This will show records 10001, 10002, ... ,20000

Saeed-rz
  • 1,435
  • 1
  • 20
  • 38
0

TSQL

SELECT TOP 10000 ...

PL/SQL

... WHERE ROWNUM < 10000 ...

kbrimington
  • 25,142
  • 5
  • 62
  • 74