informix 中查询的行号
我正在使用 informix 数据库,我想要一个查询,您也可以在查询中生成行号,
就像
select row_number(),firstName,lastName
from students;
row_number() firstName lastName
1 john mathew
2 ricky pointing
3 sachin tendulkar
这里的firstName、lastName 来自数据库,其中行号是在查询中生成的。
I am using informix database, I want a query which you could also generate a row number along with the query
Like
select row_number(),firstName,lastName
from students;
row_number() firstName lastName
1 john mathew
2 ricky pointing
3 sachin tendulkar
Here firstName, lastName are from Database, where as row number is generated in a query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 OLAP 表达式,您需要 OVER() 中包含某些内容,因为您不希望分区包含 SORT 子句,如下所示:
FROM Students;
如果您不想按名称排序,则可以使用按 ROWID 排序将记录输入系统的方式。
Using OLAP expressions you need the OVER() with something in it, since you don't want partitions include a SORT clause, like this:
FROM students;
and if you don't want to order by name, you could use the way records were entered in the system by ordering by ROWID.
选择 sum(1) over (order by rowid) as row_number, M.* from systables M
select sum(1) over (order by rowid) as row_number, M.* from systables M
我知道这是一个老问题,但由于我刚刚遇到这个问题并得到了这里未提及的灵魂,我很难分享它,所以这里是:
1-您需要创建一个返回给定范围内的数字的函数:
2-将此函数的结果与您想要的表相交叉:
唯一的事情是您必须事先知道您想要的行数,您可以使用 COUNT(*) 函数得到它。
这适用于我的 Informix 数据库,其他实现可能需要一些调整。
I know its an old question, but since i just faced this problem and got a soultion not mentioned here, i tough i could share it, so here it is:
1- You need to create a FUNCTION that return numbers in a given range:
2- You Cross the results of this Function with the table you want:
The only thing is that you must know before-hand the number of rows you want, you may get this with the COUNT(*) Function.
This works with my Informix Database, other implementations may need some tweaking.
我认为最简单的方法是使用以下代码并相应地调整其返回值。
SELECT rowid, * FROM table
它对我有用,但请注意,它将返回数据库中的行号,而不是查询中的行号。
PS这是来自专家交换的公认答案。
I think the easiest way would be to use the following code and adjust its return accordingly.
SELECT rowid, * FROM table
It works for me but please note that it will return the row number in the database, not the row number in the query.
P.S. it's an accepted answer from Experts Exchange.
给定一个名为 Table3 的表,其中包含 3 列:
注意:
seq 是表中的一个字段,具有按升序排列的唯一值。 这些数字不必是连续的。
这是返回行号(RowNum)以及查询结果的查询
Given a table called Table3 with 3 columns:
NOTE:
seq is a field within the Table that has unique values in ascending order. The numbers do not have to be contiguous.
Here is query to return a rownumber (RowNum) along with query result
您可能无法在分散在多个 DBSpace 中的表中使用 ROWID,因此任何使用 ROWID 的解决方案都不是特别可移植。 这也是强烈劝阻的。
如果您的源表中没有 SERIAL 列(这是将其作为一般概念实现的更好方法),请查看
CREATE SEQUENCE,它或多或少相当于 Orrible 函数,该函数在 SELECTed 时生成唯一编号(与 SERIAL 相反,SERIAL 在插入行时生成唯一编号)。
You may not be able to use ROWID in a table that's fragmented across multiple DBSpaces, so any solution that uses ROWID is not particularly portable. It's also strongly discouraged.
If you don't have a SERIAL column in your source table (which is a better way of implementing this as a general concept), have a look at
CREATE SEQUENCE, which is more or less the equivalent of an Orrible function that generates unique numbers when SELECTed from (as opposed to SERIAL, which generates the unique number when the row is INSERTed).
最好的方法是使用(新初始化的)序列。
The best way is to use a (newly initialized) sequence.