SELECT with an aggregate function returns the results of that aggregate function for matched rows. Regardless of whether your DBMS returns 0 or NULL or blue, no yellow auuuugh ... as the MIN(id), it still returns *something*. Therefore when you do a fetch loop, there is something to fetch (even if that something is undefined or NULL), therefore the fetch loop executes and the counter is incremented and therefore the second run prints 1.
update To be a bit clearer: it's the difference between [] and [[undef]]. With [] there is nothing to fetch. With [[undef]] there is something to fetch even though that something is nothing. If you use $dbh->selecall_arrayref() on your two queries and Data::Dumper the results you'll see that difference betweeen the returned arrayrefs.
update 2 For example:
Outputs:use DBI; my $dbh=DBI->connect('dbi:Pg:dbname=test1'); $dbh->do('CREATE TABLE x (id INT)'); my $results1 = $dbh->selectall_arrayref('SELECT * FROM x WHERE 1=0'); my $results2 = $dbh->selectall_arrayref('SELECT MIN(id) FROM x WHERE 1 +=0'); use Data::Dumper; print Dumper $results1; print Dumper $results2;
$VAR1 = []; $VAR1 = [ [ undef ] ];
In reply to Re: DBI and selecting aggregate columns with no results
by jZed
in thread DBI and selecting aggregate columns with no results
by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |