表中'next'和'previous'的 SQL 是什么?



我有一个项目表,每个项目都有一个与之关联的日期。如果我有一个与一个项目关联的日期,如何使用 SQL 查询数据库以获取表中的"上一个"和"后续"项目?

不可能简单地添加(或减去(一个值,因为日期之间没有规则的间隔。

一种可能的应用程序是相册或博客 Web 应用程序中的"上一个/下一个"链接,其中基础数据位于 SQL 表中。

我认为有两种可能的情况:

首先,每个日期都是唯一的:

示例数据:

1,3,8,19,67,45

当提供 8 作为参数时,哪个查询(或多个查询(会给出 3 和 19?(或第 3、8、19 行(。请注意,并不总是有三行要返回 - 在序列的末尾会缺少一行。

其次,如果有一个单独的唯一键来对元素进行排序,那么返回"围绕"日期的集合的查询是什么?预期的顺序是按日期然后键。

示例数据:

(key:date) 1:1,2:3,3:8,4:8,5:19,10:19,11:67,15:45,16:8

对"8"的哪个查询返回集合:

2:3,3:8,4:8,16:8,5:19

或者哪个查询生成表:

key date prev-key next-key
1   1    null     2
2   3    1        3
3   8    2        4
4   8    3        16
5   19   16       10
10  19   5        11
11  67   10       15
15  45   11       null
16  8    4        5

表顺序并不重要 - 重要的是下一个键和上一个键字段。


TheSoftwareJedi和Cade Roux都有适用于我昨晚发布的数据集的解决方案。对于第二个问题,对于这个数据集,这两个问题似乎都失败了:

(key:date) 1:1,2:3,3:8,4:8,5:19,10:19,11:67,15:45,16:8

预期的顺序是按日期然后键,因此一个预期结果可能是:

2:3,3:8,4:8,16:8,5:19

另一个:

key date prev-key next-key
1   1    null     2
2   3    1        3
3   8    2        4
4   8    3        16
5   19   16       10
10  19   5        11
11  67   10       15
15  45   11       null
16  8    4        5

表顺序并不重要 - 重要的是下一个键和上一个键字段。

从元素<8 的数据中选择 max(element(

联盟

从元素> 8 的数据中选择最小值(元素(

但一般来说,将 sql 用于面向集合的操作比将 SQL 视为迭代操作更有用。

自加入。

对于表格:

/*
CREATE TABLE [dbo].[stackoverflow_203302](
    [val] [int] NOT NULL
) ON [PRIMARY]
*/

带参数@val

SELECT cur.val, MAX(prv.val) AS prv_val, MIN(nxt.val) AS nxt_val
FROM stackoverflow_203302 AS cur
LEFT JOIN stackoverflow_203302 AS prv
    ON cur.val > prv.val
LEFT JOIN stackoverflow_203302 AS nxt
    ON cur.val < nxt.val
WHERE cur.val = @val
GROUP BY cur.val

您可以将其设置为具有输出参数的存储过程,或者只是将其作为关联子查询连接到要拉取的数据。

如果没有该参数,对于您的数据,结果将是:

val         prv_val     nxt_val
----------- ----------- -----------
1           NULL        3
3           1           8
8           3           19
19          8           45
45          19          67
67          45          NULL

对于修改后的示例,您可以使用此子查询作为相关子查询:

/*
CREATE TABLE [dbo].[stackoverflow_203302](
    [ky] [int] NOT NULL,
    [val] [int] NOT NULL,
    CONSTRAINT [PK_stackoverflow_203302] PRIMARY KEY CLUSTERED (
        [ky] ASC
    )
)
*/
SELECT cur.ky AS cur_ky
        ,cur.val AS cur_val
        ,prv.ky AS prv_ky
        ,prv.val AS prv_val
        ,nxt.ky AS nxt_ky
        ,nxt.val as nxt_val
FROM (
    SELECT cur.ky, MAX(prv.ky) AS prv_ky, MIN(nxt.ky) AS nxt_ky
    FROM stackoverflow_203302 AS cur
    LEFT JOIN stackoverflow_203302 AS prv
        ON cur.ky > prv.ky
    LEFT JOIN stackoverflow_203302 AS nxt
        ON cur.ky < nxt.ky
    GROUP BY cur.ky
) AS ordering
INNER JOIN stackoverflow_203302 as cur
    ON cur.ky = ordering.ky
LEFT JOIN stackoverflow_203302 as prv
    ON prv.ky = ordering.prv_ky
LEFT JOIN stackoverflow_203302 as nxt
    ON nxt.ky = ordering.nxt_ky

输出符合预期:

cur_ky      cur_val     prv_ky      prv_val     nxt_ky      nxt_val
----------- ----------- ----------- ----------- ----------- -----------
1           1           NULL        NULL        2           3
2           3           1           1           3           8
3           8           2           3           4           19
4           19          3           8           5           67
5           67          4           19          6           45
6           45          5           67          NULL        NULL

在 SQL Server 中,我更喜欢将子查询设置为公用表表达式。 这使得代码看起来更线性,嵌套更少,如果有很多嵌套,则更容易遵循(此外,在某些重新连接时需要更少的重复(。

首先,这应该有效(ORDER BY 很重要(:

select min(a)
from theTable
where a > 8
select max(a)
from theTable
where a < 8

对于我恳求你问的第二个问题......:

    select * 
    from theTable
    where date = 8
    union all
    select *
    from theTable
    where key = (select min(key) 
                 from theTable
                 where key > (select max(key)
                              from theTable
                              where date = 8)
                )
    union all
    select *
    from theTable
    where key = (select max(key) 
                 from theTable
                 where key < (select min(key)
                              from theTable
                              where date = 8)
                )
    order by key
SELECT 'next' AS direction, MIN(date_field) AS date_key
FROM table_name
  WHERE date_field > current_date
GROUP BY 1 -- necessity for group by varies from DBMS to DBMS in this context
UNION
SELECT 'prev' AS direction, MAX(date_field) AS date_key
  FROM table_name
  WHERE date_field < current_date
GROUP BY 1
ORDER BY 1 DESC;

生产:

direction  date_key
---------  --------
prev              3
next             19

我自己尝试的基于软件绝地的设置解决方案。

第一个问题:

select date from test where date = 8
union all
select max(date) from test where date < 8
union all
select min(date) from test where date > 8
order by date;

第二个问题:

在调试时,我使用了数据集:

(key:date) 1:1,2:3,3:8,4:8,5:19,10:19,11:67,15:45,16:8,17:3,18:1

要给出此结果:

select * from test2 where date = 8
union all
select * from (select * from test2
                   where date = (select max(date) from test2 
                                     where date < 8)) 
    where key = (select max(key) from test2 
                    where date = (select max(date) from test2 
                                      where date < 8))
union all
select * from (select * from test2
                   where date = (select min(date) from test2 
                                     where date > 8)) 
    where key = (select min(key) from test2 
                    where date = (select min(date) from test2 
                                      where date > 8))
order by date,key;

在这两种情况下,严格来说,最终的按子句排序是可选的。

如果你的RDBMS支持LAG和LEAD,这很简单(Oracle,PostgreSQL,SQL Server 2012(

这些允许在单个查询中选择任何给定行的两侧的行

试试这个...

SELECT TOP 3 * FROM YourTable
WHERE Col >= (SELECT MAX(Col) FROM YourTable b WHERE Col < @Parameter)
ORDER BY Col

最新更新