MySQL 如何从mysql中的表中选择N条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8805538/
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 22:18:14  来源:igfitidea点击:

How to select N records from a table in mysql

mysql

提问by Murthy

How can I get only 10 records from a table where there are more than 1000 records. I have a test table with rowid, name, cost.

如何从超过 1000 条记录的表中仅获取 10 条记录。我有一个包含 rowid、name、cost 的测试表。

   select  name, cost from test;

here I want to select only first 10 rows and dont want to select rowid.

在这里我只想选择前 10 行,不想选择 rowid。

回答by 472084

To select the first ten records you can use LIMIT followed by the number of records you need:

要选择前十个记录,您可以使用 LIMIT 后跟您需要的记录数:

SELECT name, cost FROM test LIMIT 10

To select ten records from a specific location, you can use LIMIT 10, 100

要从特定位置选择十条记录,您可以使用 LIMIT 10, 100

SELECT name, cost FROM test LIMIT 100, 10

This will display records 101-110

这将显示记录 101-110

SELECT name, cost FROM test LIMIT 10, 100

This will display records 11-111

这将显示记录 11-111

To make sure you retrieve the correct results, make sure you ORDER BY the results too, otherwise the returned rows may be random-ish

为了确保您检索到正确的结果,请确保您也按结果排序,否则返回的行可能是随机的

You can read more @ http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

你可以阅读更多@ http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

回答by RedFilter

You should have an ORDER BYclause when you use LIMIT, so that you will get the same recordset if you call it two times in succession and no data has changed.

ORDER BY使用时应该有一个子句LIMIT,这样如果连续调用它两次并且没有数据更改,您将获得相同的记录集。

So, do something like:

因此,请执行以下操作:

select  name, cost 
from test 
order by rowid
limit 10; 

回答by adelphus

SELECT TOP(10) name, cost FROM test;

回答by Vipin Pandey

Using this query, you can get first 10 records.

使用此查询,您可以获得前 10 条记录。

SELECT employee_id, first_name 
FROM employees  
LIMIT 10;