在 MySQL 中限制 SQL 查询结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:45:22  来源:igfitidea点击:

Limit SQL query result in MySQL

sqlmysqlpagination

提问by chcne

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

我想限制我在 MySQL 中获取的行数。你能告诉我怎么做吗?

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
  • 第一个查询我只想检索前 10,000 条记录
  • 第二个查询我只想检索 10,000 - 20,000 中的记录

etc

等等

回答by In silico

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

您正在寻找的术语是“分页”。不幸的是,这取决于 SQL 引擎。

For MS SQL Server, see this Stack Overflow question.

对于 MS SQL Server,请参阅此堆栈溢出问题

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

既然你提到了MySQL,其实很简单:

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.

第一条语句提取结果 1-10,000,第二条语句提取结果 10,001-20,000。

回答by Dora

I think the following queries will give you the desired result

我认为以下查询会给你想要的结果

SELECT * FROM PERSON_TBL LIMIT 0, 10000

SELECT * FROM PERSON_TBL LIMIT 0, 10000

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

@第一个查询我只想检索前 10,000 条记录

SELECT * FROM PERSON_TBL LIMIT 10000,10000

SELECT * FROM PERSON_TBL LIMIT 10000,10000

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

@ 第二个查询我只想检索 10,000 - 20,000 条记录

回答by Jay

select top x * from table in SQL Server

select * from table where ROWNUM < x in Oracle

select * from table limit x in MySQL

回答by miku

MySQL and PostgreSQL support OFFSETthat is usually used with a LIMITclause.

OFFSET通常与LIMIT子句一起使用的MySQL 和 PostgreSQL 支持。

SELECT column FROM table
LIMIT 10000

SELECT column FROM table
LIMIT 10000 OFFSET 10000

回答by indianwebdevil

in mysql you do as follows

在mysql中你做如下

SELECT * FROM PERSON_TBL LIMIT 0, 1000 

SELECT * FROM PERSON_TBL LIMIT 1000, 1000 

Query 1 will fetch first 1000 records,

查询 1 将获取前 1000 条记录,

Query 2 will fetch next 1000 records

查询 2 将获取接下来的 1000 条记录

Syntax for limits clause

限制子句的语法

LIMITS OFFSET, ROWCOUNT

限制偏移,行数

Where ROWCOUNT give number of row to fetch

其中 ROWCOUNT 给出要获取的行数

OFFSET gives from which row to fetch more info here

OFFSET给出了该行以获取更多信息点击这里

回答by kbrimington

TSQL

TSQL

SELECT TOP 10000...

SELECT TOP 10000...

PL/SQL

PL/SQL

... WHERE ROWNUM < 10000...

... WHERE ROWNUM < 10000...

回答by Saeed-rz

in MySQL :

在 MySQL 中:

SELECT * FROM `your_table` LIMIT 0, 10000 

This will display the first 10000 results from the database.

这将显示数据库中的前 10000 个结果。

 SELECT * FROM `your_table` LIMIT 10000, 20000 

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

这将显示记录 10001, 10002, ... ,20000