在SQL中,如何获取按特定列排序的前N行? [英] In SQL, how do you get the top N rows ordered by a certain column?

查看:137
本文介绍了在SQL中,如何获取按特定列排序的前N行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个表的前N行,并按其中一列的数字排序.

I want to select the top N rows of a table, ranked by how high the number in one of their columns is.

我已经有:

SELECT * FROM movie ORDER BY worldwide_gross DESC;

如何获得前二十个?

如果有什么不同,我正在使用MySQL.

If it makes any difference I'm using MySQL.

干杯!

推荐答案

定义:Limit用于将您的MySQL查询结果限制为指定范围内的结果.您可以使用它显示前X个结果数,或显示X-Y个结果的范围.它被表述为Limit X,Y,并包含在查询的末尾. X是起点(记住第一条记录为0),Y是持续时间(要显示多少条记录). 也称为:范围结果 例子:

Definition: Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display). Also Known As: Range Results Examples:

SELECT * FROM `your_table` LIMIT 0, 10 

这将显示数据库中的前10个结果.

This will display the first 10 results from the database.

SELECT * FROM `your_table` LIMIT 5, 5 

这将显示记录6、7、8、9和10

This will show records 6, 7, 8, 9, and 10

来自 About.com

这篇关于在SQL中,如何获取按特定列排序的前N行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆