获取最大值之前的值
例如,给定这个稀疏 id 表:
|id| | 1| | 2| | 3| | 6| | 7|
我可以使用以下查询从表中获取最高的“id”:
SELECT max(id) FROM Comics
我得到:
|id| | 7|
如何获取最高“id”之前的“id”(即使这些值是“ t 连续)?
For example, given this table of sparse ids:
|id| | 1| | 2| | 3| | 6| | 7|
I can obtain the highest "id" from my table using this query:
SELECT max(id) FROM Comics
I get:
|id| | 7|
How can I get the "id" just preceding the highest "id" (even if the values aren't continuous)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,您可以首先找到最大 id(您已完成),然后找到小于 (
<
) 最大值的最大 id。具体来说,
您也可以按降序对结果进行排序:
然后查看返回的第二行。
In general terms, you could first find the maximum id (which you've done), then find the maximum id that is less than (
<
) the maximum.Specifically,
Alternately, you can order the results in descending order:
and then look at the second row returned.
与注释相同
:这是 transact sql 语法,根据您的供应商使用 rownum 或 limit
来获取您可以执行的第 2 行
is the same as
note: this is transact sql syntax, use rownum or limit depending on your vendor
to get row 2 you can do
这也可以做到..
this would do it as well ..