【问题标题】:Perl DBI/PostgreSQL min functionPerl DBI/PostgreSQL 最小函数
【发布时间】:2015-05-11 01:55:16
【问题描述】:

我正在使用 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";

它在 postgresql 编辑器中有效,但在 perl 中无效。任何帮助将不胜感激!

【问题讨论】:

    标签: perl postgresql dbi


    【解决方案1】:

    您的问题是结果集中没有col_id 列。您正在计算min(col_id),所以$results 将有一个min 键,而不是col_id 键。你想说:

    $id = $results->{min};
    

    提取您正在寻找的价值。或者您可以在 SELECT 中添加别名:

    $sth = $dbh->prepare("
        select min(col_id) as min_col_id
        from table
        where col_num = x
    ") or die $dbh->errstr;
    

    并在查看 $results 时使用该名称:

    $id = $results->{min_col_id};
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2022-01-19
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2014-02-07
      • 2019-10-04
      • 2020-04-12
      相关资源
      最近更新 更多