gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:00:07 26 4
gpt4 key购买 nike

我目前正在尝试查询表中所有 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

关于mysql - 显示带有限制的最后 x 行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com