选择特定ID后面的行

时间:2013-08-01 07:48:34

标签: sql sql-server tsql sql-server-2012

我有以下查询,它从新闻文章表中选择。由于多种排序因素,我找不到下一页的日期。

我拥有的参数是查询结果集中的最后一个ID,所以如何获取下一页... 再次,我没有页面索引,我只有最后一个ID来自上一个查询结果集

SELECT TOP (20) ID, 
                Datediff(SECOND, CONVERT(DATETIME, '1970-01-01', 102), 
                RELEASE_DATE) AS date, 
                Datediff(SECOND, CONVERT(DATETIME, '1970-01-01', 102), EDIT_DATE 
                )    AS lastchanged, 
                IMPORTANT 
FROM   ARTICLES AS news WITH (NOLOCK) 
WHERE  ( STATUS = 1 ) 
       AND ( RELEASE_DATE <= Getutcdate() ) 
ORDER  BY IMPORTANT DESC, 
          DATE DESC, 
          LASTCHANGED DESC 

这是我的数据的一个例子

  id       date     lastchanged important
187346  1366070400  1345212540  1
187306  1365984000  1345290300  1
187330  1365984000  1342457880  1
187344  1363392000  1342461540  1
187343  1363392000  1342461300  1
187342  1363392000  1342459980  1
187339  1363392000  1342459800  1
187337  1363392000  1342458960  1
187335  1363392000  1342458720  1
187334  1363392000  1342458600  1
187332  1363392000  1342458060  1
187331  1363392000  1342457940  1
187327  1363392000  1342457340  1
187328  1363392000  1342457340  1
187326  1363392000  1342456860  1
187323  1363392000  1342456020  1
187322  1363392000  1342455480  1
187321  1363392000  1342454700  1
187316  1363392000  1342454580  1
187320  1363392000  1342454520  1

2 个答案:

答案 0 :(得分:2)

如果您想要做的是在某个应用程序中一次显示20个帖子,并且能够获取接下来的20个帖子,直到结果结束,可能使用OFFSET FETCH clause可能有效。

初始查询将是:

SELECT        
    id, DATEDIFF(SECOND, CONVERT(DATETIME, '1970-01-01', 102), release_date) AS date,  DATEDIFF(SECOND, CONVERT(DATETIME, '1970-01-01', 102), edit_date) AS lastchanged, important
FROM articles AS news WITH (NOLOCK)
WHERE (status = 1) AND (release_date <= GETUTCDATE())
ORDER BY important DESC, date DESC, lastchanged DESC
OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY;

以及后续查询会增加offset值:

SELECT        
    id, DATEDIFF(SECOND, CONVERT(DATETIME, '1970-01-01', 102), release_date) AS date,  DATEDIFF(SECOND, CONVERT(DATETIME, '1970-01-01', 102), edit_date) AS lastchanged, important
FROM articles AS news WITH (NOLOCK)
WHERE (status = 1) AND (release_date <= GETUTCDATE())
ORDER BY important DESC, date DESC, lastchanged DESC
OFFSET 20 ROWS FETCH NEXT 20 ROWS ONLY;

答案 1 :(得分:1)

;WITH CTE( ROW,ID, 
                [date], 
                [lastchanged], 
                IMPORTANT ) AS
(
SELECT rOW_NUMBER() OVER(oRDER BY IMPORTANT DESC, 
          DATE DESC, 
          LASTCHANGED DESC ) AS rOW,ID, 
                Datediff(SECOND, CONVERT(DATETIME, '1970-01-01', 102), 
                RELEASE_DATE) AS date, 
                Datediff(SECOND, CONVERT(DATETIME, '1970-01-01', 102), EDIT_DATE 
                )    AS lastchanged, 
                IMPORTANT 
FROM   ARTICLES AS news WITH (NOLOCK) 
WHERE  ( STATUS = 1 ) 
       AND ( RELEASE_DATE <= Getutcdate() ) 
                ) 

SELECT TOP 20 ID, 
                [date], 
                [lastchanged], 
                IMPORTANT FROM CTE WHERE rOW>cOALESCE((sELECT rOW FROM CTE WHERE iD=1871),0);

以下是此示例。在示例中,我只选择前5名,因此输入任何ID,您将看到它将获取接下来的5条记录。

Sql Fiddle Demo - 首先输入错误的ID,就像我在这里进行-1而不是获得前五个记录。

Sql Fiddle Demo - 输入id = 187339,因为它是第一页的最后一个ID

Sql Fiddle Demo - 输入id = 187331,因为它是第二页的最后一个ID,依此类推