0

我有一个按非主键排序的数据表,例如

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

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

4

1 回答 1

0

您可以在查询结束时使用限制,例如

  SELECT * FROM YOUR_TABLE LIMIT 2,1
于 2013-07-18T04:33:52.773 回答