gpt4 book ai didi

sql - 使用不带 ORDER BY ID 的 Row_Number() 选择行

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:40 24 4
gpt4 key购买 nike

我正在尝试选择特定行号之间的行(例如 1 到 50 或 51 到 10 等)。

下面是我的表 MatTk 架构,所有列都允许空值:

tk_id [varchar(50)] | mat_id [varchar(50)] | ven_id [varchar(50)] | tk_rate [money]
1023 104 2212 120.11

本文Select subset of rows using Row_Number() 建议 ORDER BY id 但我想避免它并以表数据的自然顺序显示它。

同时查看这篇文章 SQL Server 2005 ROW_NUMBER() without ORDER BY这建议插入临时表,但它不是一个选项,因为 MatTk 表有数百万行。

有没有办法在不按 ID 排序且不创建临时表的情况下按行号查询行?

最佳答案

This article Select subset of rows using Row_Number() suggests ORDER BY id but I want to avoid it and show it in the natural order of the table data.

不存在“表格数据的自然顺序”这样的东西。
数据库表本质上是无序的。

这意味着从没有 order by 子句的 select 语句返回的行是任意排序的(注意任意与随机不同)。有关详细信息,请阅读 Michael J. Swart 的 Without ORDER BY, You Can’t Depend On the Order of Results

如果您不关心row_number函数中数字的顺序,您可以使用

row_number() over(order by (select null))

但是请注意,使用它会返回任意行号 - 这意味着每次运行查询时不能相信它们会保持不变。

关于sql - 使用不带 ORDER BY ID 的 Row_Number() 选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50241753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com