假设表中没有有序列,则使用Oracle,MYSQL和SQL Server中的SQL从表中选择第n行[duplicate]

时间:2022-05-10 15:43:13

This question already has an answer here:

这个问题在这里已有答案:

Let us take a table t1

我们来一个表t1

SQL > Select * from t1;

COL1

    9
    1
    8
    6
    4

Q) Query to retrieve third row from the table.

Q)查询从表中检索第三行。

A) Oracle :

A)Oracle:

SQL > select col1 from (select col1, row_number() over(order by rowid) rn from t1) where rn=3;

As rowid psuedo column doesn't exist in other databases, how can retrieve a nth record from a table in databases like MYSQL, SQL Server etc.

由于rowid psuedo列在其他数据库中不存在,如何从MYSQL,SQL Server等数据库中的表中检索第n条记录。

2 个解决方案

#1


0  

SQL Server also, you have the ROW_NUMBER Function

SQL Server也有,你有ROW_NUMBER函数

You Can Go like

你可以去

;WITH CTE
AS
(
    SELECT
        RN = ROW_NUMBER() OVER(ORDER BY EmployeeID),
        *
        FROM dbo.Employee
)
SELECT
    *
    FROM CTE
        WHERE RN = 5--To Get the 5th Record

#2


0  

for mssql you can use row_number() window funciton

对于mssql,您可以使用row_number()窗口函数

select * from 
(
select *, row_number() over(order by col1) rn from t
) t1 where t1.rn=1 -- 2 or 3, n

#1


0  

SQL Server also, you have the ROW_NUMBER Function

SQL Server也有,你有ROW_NUMBER函数

You Can Go like

你可以去

;WITH CTE
AS
(
    SELECT
        RN = ROW_NUMBER() OVER(ORDER BY EmployeeID),
        *
        FROM dbo.Employee
)
SELECT
    *
    FROM CTE
        WHERE RN = 5--To Get the 5th Record

#2


0  

for mssql you can use row_number() window funciton

对于mssql,您可以使用row_number()窗口函数

select * from 
(
select *, row_number() over(order by col1) rn from t
) t1 where t1.rn=1 -- 2 or 3, n