选择MySQL中的前十行

时间:2014-08-26 13:06:50

标签: mysql sql

My Table gainfinal包含三个列 - countrycode,year和values。我想选择前十个值的十行。首先,我使用以下查询根据值创建了一个排名。

    SELECT countrycode, `values`,
    @curRank := @curRank + 1 AS rank
    FROM gainfinal CROSS JOIN
    (SELECT @curRank := 0) vars
    WHERE year = 2000
    ORDER By `values` DESC ;

现在,我需要选择排名最高的前十行。我该怎么做 ?

4 个答案:

答案 0 :(得分:4)

SELECT countrycode, `values`,
@curRank := @curRank + 1 AS rank
FROM gainfinal CROSS JOIN
(SELECT @curRank := 0) vars
WHERE year = 2000
ORDER By `values` DESC
LIMIT 10;

答案 1 :(得分:3)

使用

LIMIT
SQL中的

例如:

SELECT countrycode, `values`,
@curRank := @curRank + 1 AS rank
FROM gainfinal CROSS JOIN
(SELECT @curRank := 0) vars
WHERE year = 2000
ORDER By `values` DESC
LIMIT 10

答案 2 :(得分:2)

你可以使用:Limit

从第20行开始获得10行
LIMIT 10 OFFSET 20 --Equivalent to LIMIT 20, 10

答案 3 :(得分:1)

您还可以在where语句

中使用rank变量
...WHERE year = 2000 and @curRank <11;