我可以获取一条记录在 SQL 结果表中的位置吗? [英] Can I get the position of a record in a SQL result table?

查看:16
本文介绍了我可以获取一条记录在 SQL 结果表中的位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做类似的事情

SELECT * FROM mytable ORDER BY mycolumn ASC;

我按特定顺序得到结果表.

I get a result table in a specific order.

在 SQL 中是否有一种方法可以有效地找出给定 PK 的结果表中的哪个位置将包含我的 PK 记录?

Is there a way in SQL to efficiently find out, given a PK, what position in that result table would contain the record with my PK?

推荐答案

您可以计算您正在排序的值比您知道其键值的记录的值低的记录数:

You can count the number of records where the value that you are sorting on has a lower value than the record that you know the key value of:

select count(*)
from mytable
where mycolumn < (select mycolumn from mytable where key = 42)

这篇关于我可以获取一条记录在 SQL 结果表中的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆