显示带有限制的最后 x 行查询

Displaying last x rows of query with limit

我目前正在尝试查询 table 中 game_id 小于 1000058 且定义了 limit 的所有值。例如,如果我设置 limit 3,我想按升序获取最后 3 行。我知道可以通过设置像 limit 2, 3 这样的偏移量来完成,但我需要事先知道行号。假设我不知道行号,如何按升序显示 query 的最后 3 行? SQLFIDDLE

显示正确的值,但顺序是 DESC 而不是 ASC

SELECT * FROM games WHERE mature = true AND category_id = 1004 AND game_id < 1000058 ORDER BY game_id DESC LIMIT 3;

显示正确的值,但我需要知道偏移量的行号

SELECT * FROM games WHERE mature = true AND category_id = 1004 AND game_id < 1000058 ORDER BY game_id LIMIT 2,3;

仅按升序显示前三行

SELECT * FROM games WHERE mature = true AND category_id = 1004 AND game_id < 1000058 ORDER BY game_id LIMIT 3;

想要的结果

game_id    game_name     cat_id  mature     description                         published_date
-------    ------------  ------  ------     ---------------------------------   --------------
1000055    Battlefield4  1004    1          Published by EA Digital Illusions   2013-01-01  
1000056    Rainbow6      1004    1          Published by EUbisoft               2015-01-01
1000057    Destiny       1004    1          Published by Bungie                 2014-01-01

您可以使用嵌套查询 - 内部查询按降序排列以便您可以限制前三场比赛,然后外部查询按升序对结果进行排序:

SELECT   *
FROM     (SELECT   * 
          FROM     games 
          WHERE    mature = true AND 
                   category_id = 1004 AND 
                   game_id < 1000058 
          ORDER BY game_id DESC
          LIMIT    3) t
ORDER BY game_id ASC        

我想这就是你要找的朋友:

SELECT * FROM (
    SELECT * FROM games 
    where game_id < 1000058 
      and mature = true 
      and category_id = 1004 
   ORDER BY game_id DESC
   LIMIT 3
) sub

ORDER BY game_id ASC