SELECT without aggregate functions returns matched rows and since no rows match, there is no fetch loop and therefore the first run prints 0.

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:

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;
Outputs:
$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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.