如何获得第n条记录 [英] How to get nth record

查看:56
本文介绍了如何获得第n条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一个答案才能从SQL Server的表中获取第n条记录。如何实现这个

Ex:

Hi Everyone,
I need an answer to get nth records from the table in SQL Server. How to achieve this
Ex:

Id        Name      Age
1        Ali        25
2        John       89
3        Mitchell   67
4        Azmaim     45
5        Shahid     46
6        Qureishi   32
7        Mayank     53
8        Agarwal    39
9        Morkel     33
10       Rajnikanth 42





我需要查询才能获得第5条记录

推荐答案

你必须要意识到的第一件事是,除非你给它一个特定的ORDER BY子句,否则SQL不会以任何特定的顺序返回行:一个无序的SELECT语句可以自由返回SQL认为有效的任何顺序的行,下周可能不一样。所以,首先要决定你想要订购什么:在这种情况下我认为它是Id。

尝试这样的事情:

The first thing you have to realise is that SQL is under no compunction to return rows in any particular order unless you give it an specific ORDER BY clause: A non-ordered SELECT statement is at liberty to return rows in any order SQL deems efficient, which may not be the same next week. So, first decide what you want to order by: in this case I assume it is the Id.
Try something like this:
WITH myTableWithRows AS (
    SELECT (ROW_NUMBER() OVER (ORDER BY myTable.Id)) as row, *
    FROM myTable)
SELECT Id, [Name], Age FROM myTableWithRows WHERE row = 5


这篇关于如何获得第n条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆