简体   繁体   English

如何提取sql语句的第n行?

[英]How to extract the nth row of a sql statement?

I am trying to extract the nth row of an SQL statement (not the nth row of a table). 我正在尝试提取SQL语句的第n行(而不是表的第n行)。 Is there an easy way to run a query and read values from specific rows. 有没有一种简单的方法来运行查询并读取特定行中的值。

I have tried something similar to this but it does not work since rownum tells me what nth record it is in the table. 我已经尝试过类似的操作,但是由于rownum告诉我表中的第n条记录,因此它不起作用。

SELECT * FROM (
    SELECT
        ROW_NUMBER() OVER(ORDER BY RewardID ASC) AS rownumber,
        RewardID,
        Name,
        Description,
        Image,
        RewardType,
        price
    FROM
        Reward
) AS num 
WHERE
    RewardType = 'Electronics' and rownum = 2

Are you using Postgres Or SQLServer? 您正在使用Postgres还是SQLServer? Both have a ROW_NUMBER() window function, but the syntax, especially around subqueries, may be slightly different. 两者都具有ROW_NUMBER()窗口函数,但是语法(尤其是在子查询周围)可能略有不同。 I'll assume Postgres. 我假设Postgres。

You query looks good to me, except that the rownum = 2 in your WHERE should presumably be rownumber = 2 , since that's what you aliased the ROW_NUMBER() column as. 您的查询对我来说看起来不错,除了WHERE中的rownum = 2应该是rownumber = 2 ,因为这就是您对ROW_NUMBER()列的别名。 You also presumably don't need the RewardType and rownumber columns in your result, since their values will always be 'Electronics' and 2 respectively. 您可能还不需要结果中的RewardTyperownumber列,因为它们的值始终分别是'Electronics'2 Corrected and formatted for readability, this looks ok to me: 经过更正和格式化以提高可读性,这对我来说似乎可以:

SELECT RewardID, Name, Description, Image, RewardType, price
FROM (
    SELECT
        ROW_NUMBER() OVER(ORDER BY RewardID ASC) AS rownumber,
        RewardID,
        Name,
        Description,
        Image,
        RewardType,
        price
    FROM Reward) AS num
WHERE RewardType = 'Electronics' and rownumber = 2

The only questionable part of that is the WHERE RewardType = 'Electronics' . 唯一值得怀疑的部分是WHERE RewardType = 'Electronics' Should that actually be in the subquery rather than the outer query? 那应该真正在子查询中而不是外部查询中吗? The difference is that if it is in the subquery, the row counting will include only reward type 'Electronics', whereas in the outer query, all reward types will be counted. 区别在于,如果在子查询中,则行计数将仅包括奖励类型“ Electronics”,而在外部查询中,将对所有奖励类型进行计数。 To only count the reward type 'Electronics', modify it as so: 要仅计算奖励类型“电子”,请对其进行如下修改:

SELECT RewardID, Name, Description, Image, RewardType, price
FROM (
    SELECT
        ROW_NUMBER() OVER(ORDER BY RewardID ASC) AS rownumber,
        RewardID,
        Name,
        Description,
        Image,
        RewardType,
        price
    FROM Reward
    WHERE RewardType = 'Electronics') AS num
WHERE rownumber = 2

Edit: Since the comment you just made clarifies what you're really trying to do, I'll add that you should NOT be making an individual query for each row of the data that you want. 编辑:由于您刚才发表的评论阐明了您实际上要执行的操作,因此我要补充一点,您不应对所需数据的每一行进行单独查询。 Whatever interface you are using to your database will have a way of iterating over a query result that uses multiple rows, and if that's what you really need, you should find out how to do that. 无论您使用哪种数据库接口,都可以通过一种方法来遍历使用多行的查询结果,如果这是您真正需要的,则应该找出方法。

You can try using OFFSET/FETCH : 您可以尝试使用OFFSET/FETCH

SELECT
    RewardID,
    Name,
    Description,
    Image,
    RewardType,
    price
FROM Reward
WHERE RewardType = 'Electronics'
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY

Tell it to OFFSET (skip) row 1 then FETCH (select) 1 row after that, which would be row 2. This removes the need for a sub select. 将其告诉OFFSET (跳过)第1行,然后在其后FETCH (选择)1行(即第2行)。这消除了子选择的需要。

The key is the order by, try using null in the order by. 关键是order by,请尝试在order by中使用null。 SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY (select null)) AS rownumber, RewardID, Name, Description, Image, RewardType, price FROM Reward ) AS num WHERE RewardType = 'Electronics' and rownum = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM