从表中选择最上面的N行

时间:2022-05-08 12:55:55

I am making some paging, and I need to make some query and get the result form defined slicing . for example: I need to get all "top" rows in range 20n < x < 40n etc.

我正在做一些分页,我需要做一些查询并得到定义切片的结果表单。例如:我需要获得范围为20n < x < 40n等的所有“top”行。

SELECT * FROM Reflow  
WHERE ReflowProcessID = somenumber
ORDER BY ID DESC;

and now I need to make my sliding by column called ID .

现在我需要让我的滑动被称为ID。

Any suggestions how to so ? I need to run my query on mysql, mssql, and oracle.

有什么建议吗?我需要在mysql、mssql和oracle上运行查询。

2 个解决方案

#1


42  

Assuming your page size is 20 record, and you wanna get page number 2, here is how you would do it:

假设你的页面大小是20,而你想要的是第2页,下面是你的做法:

SQL Server, Oracle:

SQL Server,甲骨文:

SELECT *   -- <-- pick any columns here from your table, if you wanna exclude the RowNumber
FROM (SELECT ROW_NUMBER OVER(ORDER BY ID DESC) RowNumber, * 
      FROM Reflow  
      WHERE ReflowProcessID = somenumber) t
WHERE RowNumber >= 20 AND RowNumber <= 40    

MySQL:

MySQL:

SELECT * 
FROM Reflow  
WHERE ReflowProcessID = somenumber
ORDER BY ID DESC
LIMIT 20 OFFSET 20

#2


17  

In MySql, you can get 10 rows starting from row 20 using:

在MySql中,可以使用以下命令从第20行开始获得10行:

SELECT * FROM Reflow  
WHERE ReflowProcessID = somenumber
ORDER BY ID DESC
LIMIT 10 OFFSET 20 --Equivalent to LIMIT 20, 10

#1


42  

Assuming your page size is 20 record, and you wanna get page number 2, here is how you would do it:

假设你的页面大小是20,而你想要的是第2页,下面是你的做法:

SQL Server, Oracle:

SQL Server,甲骨文:

SELECT *   -- <-- pick any columns here from your table, if you wanna exclude the RowNumber
FROM (SELECT ROW_NUMBER OVER(ORDER BY ID DESC) RowNumber, * 
      FROM Reflow  
      WHERE ReflowProcessID = somenumber) t
WHERE RowNumber >= 20 AND RowNumber <= 40    

MySQL:

MySQL:

SELECT * 
FROM Reflow  
WHERE ReflowProcessID = somenumber
ORDER BY ID DESC
LIMIT 20 OFFSET 20

#2


17  

In MySql, you can get 10 rows starting from row 20 using:

在MySql中,可以使用以下命令从第20行开始获得10行:

SELECT * FROM Reflow  
WHERE ReflowProcessID = somenumber
ORDER BY ID DESC
LIMIT 10 OFFSET 20 --Equivalent to LIMIT 20, 10