
Kanji
User
Jun 29, 2000, 5:40 PM
Post #2 of 7
(1399 views)
|
You should see if you can't do this in your SQL statement, rather than in Perl. Check your database's documentation for the prefered method, but in MySQL it'd be something ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> SELECT username, SEC_TO_TIME( SUM( TIME_TO_SEC( period ) ) ) AS time FROM mytable GROUP BY username ORDER BY username</pre><HR></BLOCKQUOTE> Which, if you had a table such as this ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> +-------------+----------+ | username | period | +-------------+----------+ | kanji | 00:10:03 | | adrockjames | 00:07:33 | | adrockjames | 00:07:27 | | kanji | 00:05:13 | +-------------+----------+</pre><HR></BLOCKQUOTE> ... would return the following rows ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> +-------------+----------+ | username | time | +-------------+----------+ | adrockjames | 00:15:00 | | kanji | 00:15:16 | +-------------+----------+</pre><HR></BLOCKQUOTE> If you really, really, really want to do it in Perl, then I'd suggest keeping a tally in the form as a hash as you iterate over the results ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> my %tally; while(my($user,$time) = $sth->fetchrow_array) { $seconds = hhmmss_to_ss($time); $tally{$u} += $seconds; }</pre><HR></BLOCKQUOTE> hhmmss_to_ss() is something I leave as an exercise to you to write. ;^)
|