Lemmor M.
Lemmor M.

Reputation: 1

How to loop mysql query on perl with date range

Is it possible to loop the query by range of dates?

START DATE: table_19910101

END DATE: table_19910131

sub verify {

    &db_connect();

    print "Trigger count 19910101: \n";
    $QRY = "SELECT trigger,count(1) FROM table_19910101 
            WHERE trigger IN ('B1','B2');";
    $sth = $dbh->prepare($QRY);
    $sth->execute();
    my $trigger = $sth->fetchall_arrayref();
    foreach my $row (@$trigger) {
        print join(" ", @$row), "\n";
    }
}

Upvotes: 0

Views: 76

Answers (1)

Dave Cross
Dave Cross

Reputation: 69264

The fact that you're just looking for tables from a single month makes this easier.

foreach (19910101 .. 19911031) {
  say $_;
}

If you were going over a month boundary, then the range operator would start to generate invalid dates.

Your subroutine uses a variable ($dbh) which it doesn't declare. That's bad programming practice. I suspect that the variable is created as a global in db_connect(). It would be a far better idea to return the handle from that subroutine.

my $dbh = db_connect();

Also, please remove the & from subroutine calls. It generally hasn't been needed since Perl 5 was released in 1994.

Upvotes: 3

Related Questions