[英]Perl DBI/PostgreSQL min function
I'm using Perl DBI/PostgreSQL, and I want to retrieve a minimum value in a column but I get a 'Use of uninitialized value $id in concatenation (.) or string' message for the following code: 我正在使用Perl DBI / PostgreSQL,并且想检索列中的最小值,但是对于以下代码,我收到“在连接(。)或字符串中使用未初始化的值$ id”的消息:
my $id = 0;
$sth = $dbh->prepare("
select min(col_id)
from table
where col_num = x
") or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my $results = $sth->fetchrow_hashref) {
$id = $results->{col_id};
}
print "$id";
It works in the postgresql editor, but not in perl. 它可以在postgresql编辑器中工作,但不能在perl中工作。 Any help will be appreciated! 任何帮助将不胜感激!
Your problem is that there is no col_id
column in your result set. 您的问题是结果集中没有col_id
列。 You're computing min(col_id)
so $results
will have a min
key, not a col_id
key. 您正在计算min(col_id)
因此$results
将具有min
键,而不是col_id
键。 You want to say: 你想说:
$id = $results->{min};
to extract the value you're looking for. 提取您要寻找的价值。 Or you could add an alias in your SELECT: 或者,您可以在SELECT中添加一个别名:
$sth = $dbh->prepare("
select min(col_id) as min_col_id
from table
where col_num = x
") or die $dbh->errstr;
and use that name when looking at $results
: 并在查看$results
时使用该名称:
$id = $results->{min_col_id};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.