gpt4 book ai didi

perl - SQLite:如何获取绝对行号

转载 作者:行者123 更新时间:2023-12-03 19:36:45 24 4
gpt4 key购买 nike

我试图获取查询(在Perl中)返回的行后的绝对记录数。假设我查询表如下:

my $stmt = "SELECT * FROM MAIN WHERE item='widget' LIMIT 1;";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute();


如何找出返回行的绝对行号?我尝试使用ROWID:

my $stmt = "SELECT ROWID, * FROM MAIN WHERE item='widget' ORDER BY ROWID LIMIT 1;";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute();
my $row = $sth->fetchrow_hashref();

if( defined( $row ) ) {
$RecordNumber = $row->{'rowid'};
}


如果您从未从表中删除行,则此方法有效。

但是行ID永远不会改变。因此,如果您从表中删除行,则行标识将不再与表中的绝对行号匹配。

我希望我的问题很清楚(英语不是我的母语)。

最佳答案

让我们来看这个例子

sqlite> create table my_example (field1);
sqlite> insert into my_example values ('abc');
sqlite> insert into my_example values ('booooo');
sqlite> insert into my_example values ('3231-556');
sqlite> .mode column
sqlite> .header on
sqlite> select * from my_example;
field1
----------
abc
booooo
3231-556
sqlite> select rowid, * from my_example;
rowid field1
---------- ----------
1 abc
2 booooo
3 3231-556
sqlite> delete from my_example where field1='abc';
sqlite> select rowid, * from my_example;
rowid field1
---------- ----------
2 booooo
3 3231-556
sqlite> insert into my_example values ('abc');
sqlite> select rowid, * from my_example;
rowid field1
---------- ----------
2 booooo
3 3231-556
4 abc
sqlite>


一种获得我想要实现的目标的方法(类似于rown_num)将是:

sqlite> select (select count(*) from my_example b where a.rowid >= b.oid ) as cnt, * from my_example a;
cnt field1
---------- ----------
1 booooo
2 3231-556
3 abc
sqlite>


希望这可以帮助

关于perl - SQLite:如何获取绝对行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168091/

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