What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?

后端 未结 9 1731
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:12

I\'m building a quick csv from a mysql table with a query like:

select DATE(date),count(date) from table group by DATE(date) order by date asc;
9条回答
  •  醉话见心
    2020-11-22 06:44

    not dumb, this isn't something that MySQL does, inserting the empty date values. I do this in perl with a two-step process. First, load all of the data from the query into a hash organised by date. Then, I create a Date::EzDate object and increment it by day, so...

    my $current_date = Date::EzDate->new();
    $current_date->{'default'} = '{YEAR}-{MONTH NUMBER BASE 1}-{DAY OF MONTH}';
    while ($current_date <= $final_date)
    {
        print "$current_date\t|\t%hash_o_data{$current_date}";  # EzDate provides for     automatic stringification in the format specfied in 'default'
        $current_date++;
    }
    

    where final date is another EzDate object or a string containing the end of your date range.

    EzDate isn't on CPAN right now, but you can probably find another perl mod that will do date compares and provide a date incrementor.

提交回复
热议问题