选择mysql子选择中的下一行

时间:2013-07-18 04:25:33

标签: mysql select row-number

我有一张由非主键排序的数据表,例如

id |   likes
4  |     6  
2  |     5
5  |     2
3  |     2
1  |     2

我需要一个查询来查找id#5之后的行,即id#3。

我尝试过使用行号并写下来但这看起来效率很低

select * from (
                SELECT  l.id,
                l.likes,
                @curRow := @curRow + 1 AS row_number
                FROM    sk_posters l
                JOIN    (SELECT @curRow := 0) r
                WHERE active = 'yes'
                order by likes desc, id desc)
            as mycount where row_number =
            (select row_number from (
                SELECT  l.id,
                l.likes,
                @curRow := @curRow + 1 AS row_number
                FROM    sk_posters l
                JOIN    (SELECT @curRow := 0) r
                WHERE active = 'yes'
                order by likes desc, id desc)
            as mycount
            where id=5)+1 limit 1

如果有更好,更有效的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以在查询结尾处使用限制,例如

  SELECT * FROM YOUR_TABLE LIMIT 2,1