代码之家  ›  专栏  ›  技术社区  ›  Matt

SQL排序有限制吗?(不平凡)

  •  5
  • Matt  · 技术社区  · 17 年前
    table {
      id: long
      name: string
    }
    
    1235 Fred
    1902 Trever
    5123 George
    6467 Derek
    7868 Joe
    8972 Bob
    9272 Alf
    9842 Hank
    

    5123 George
    6467 Derek
    

    1. 返回不正确的行:

      从id为<的表中选择*;7868按id排序asc限制2

    3 回复  |  直到 17 年前
        1
  •  5
  •   Alex Martelli    17 年前
    SELECT * FROM
      (select * from table where id<7868 order by id desc limit 2) AS foo
    ORDER BY ID ASC
    
        2
  •  1
  •   Jonathan Fingland    17 年前

    Select * from (
        select * from table with id<7868 
        order by id desc limit 2
    ) as t order by id asc
    

    执行子查询可以让您首先获得正确的行,然后可以在之后对其重新排序

        3
  •  0
  •   araqnid    17 年前

    从“表”中选择*,其中id<7868按id排序asc限制2偏移2

    同样,在MySQL中(我相信)“限制2,2”

    “LIMIT 2 OFFSET 2”在SQLite中也适用,至少在我尝试的版本(3.6.13)中是这样