SQL 查询选择“下一步”记录(类似于First或Top N)

发布于 2024-12-08 13:53:57 字数 336 浏览 1 评论 0原文

如果某个记录不存在,我需要执行查询以返回下一条(或上一条)记录。例如,考虑下表:

ID (primary key)    value
1                    John
3                    Bob
9                    Mike
10                   Tom.

如果 7 不存在,我想查询 id 为 7 或更大的记录。

我的问题是,

  1. 这些类型的查询是否可以使用 SQL 实现?
  2. 此类查询在数据库世界中被称为什么?

谢谢!

I need to do a query to return the next (or prev) record if a certain record is not present. For instance consider the following table:

ID (primary key)    value
1                    John
3                    Bob
9                    Mike
10                   Tom.

I'd like to query a record that has id 7 or greater if 7 is not present.

My questions are,

  1. Are these type of queries possible with SQL?
  2. What are such queries called in the DB world?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深如许 2024-12-15 13:53:57

是的,这是可能的,但实施将取决于您的 RDBMS。

在 MySQL、PostgreSQL 和 SQLite 中:

select ID, value
from YourTable
where id >= 7
order by id
limit 1

在 MS SQL-Server、Sybase 和 MS-Access 中:

select top 1 ID, value
from YourTable
where id >= 7
order by id

在 Oracle 中:

select * from (
    select ID, value
    from YourTable
    where id >= 7 
    order by id
)
where rownum = 1

在 Firebird 和 Informix 中:

select first 1 ID, value
from YourTable
where id >= 7
order by id

在 DB/2 中(此语法符合 SQL-2008 标准):

select id, value
from YourTable
where id >= 7
order by id
fetch first 1 rows only

在这些 RDBMS 中具有“窗口”函数(在 SQL-2003 标准中):

select ID, Value
from (
  select 
    ROW_NUMBER() OVER (ORDER BY id) as rownumber,
    Id, Value
  from YourTable
  where id >= 7
) as tmp                  --- remove the "as" for Oracle
where rownumber = 1

如果您不确定您拥有哪个 RDBMS:

select ID, value
from YourTable
where id = 
      ( select min(id)
        from YourTable
        where id >= 7
      )

Yes, it's possible, but implementation will depend on your RDBMS.

Here's what it looks like in MySQL, PostgreSQL and SQLite:

select ID, value
from YourTable
where id >= 7
order by id
limit 1

In MS SQL-Server, Sybase and MS-Access:

select top 1 ID, value
from YourTable
where id >= 7
order by id

In Oracle:

select * from (
    select ID, value
    from YourTable
    where id >= 7 
    order by id
)
where rownum = 1

In Firebird and Informix:

select first 1 ID, value
from YourTable
where id >= 7
order by id

In DB/2 (this syntax is in SQL-2008 standard):

select id, value
from YourTable
where id >= 7
order by id
fetch first 1 rows only

In those RDBMS that have "window" functions (in SQL-2003 standard):

select ID, Value
from (
  select 
    ROW_NUMBER() OVER (ORDER BY id) as rownumber,
    Id, Value
  from YourTable
  where id >= 7
) as tmp                  --- remove the "as" for Oracle
where rownumber = 1

And if you are not sure which RDBMS you have:

select ID, value
from YourTable
where id = 
      ( select min(id)
        from YourTable
        where id >= 7
      )
诗笺 2024-12-15 13:53:57

对于 MS-SQL

SELECT TOP 1 
id, value 
FROM your_table
WHERE id >= 7
ORDER BY id

或 MySql尝试此操作

SELECT id, value 
FROM your_table
WHERE id >= 7
ORDER BY id
LIMIT 0,1

Try this for MS-SQL:

SELECT TOP 1 
id, value 
FROM your_table
WHERE id >= 7
ORDER BY id

or for MySql

SELECT id, value 
FROM your_table
WHERE id >= 7
ORDER BY id
LIMIT 0,1
我喜欢麦丽素 2024-12-15 13:53:57

我会简单地这样做:

select top 1 * from myTable where id >=7
order by id

top 1部分的实现是T-SQL(MSSQL/Sybase),其他实现有所不同,但它总是可能的(mysql/postgre LIMIT 1,oracle rownum = 1

I would simply do it like this:

select top 1 * from myTable where id >=7
order by id

implementation of the top 1 part is T-SQL (MSSQL/Sybase), other implementations vary but it is always possible (mysql/postgre LIMIT 1, oracle rownum = 1)

隔岸观火 2024-12-15 13:53:57
select top 1 * from Persons where Id >= @Id order by Id
select top 1 * from Persons where Id >= @Id order by Id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文