You're wanting to check out the DBI module to get your connection to the database. Here is a simple way of going about it; I'm sure you can do the differencing within the SQL, but I never did go and learn SQL functions :). So this is one way of going about it:
=for SQL Table ------------------------------------ | theIDNum | theDate | thePrice | ------------------------------------ | ax1 | 2004-12-01 | 300.00 | | ax2 | 2004-12-01 | 500.00 | | ax1 | 2004-12-02 | 200.00 | | ax1 | 2004-12-03 | 100.00 | | ax3 | 2004-12-01 | 300.00 | | ax2 | 2004-12-01 | 400.00 | ------------------------------------ =cut use strict; use DBI; my $dbh = DBI->connect('dbi:mysql:database', 'u', 'p', { RaiseError => 1, AutoCommit => 1 } ); my $rows = $dbh->selectall_arrayref( 'SELECT thePrice FROM theTable ORDER BY ' . 'theDate DESC, thePrice LIMIT 2' ); my $diff = abs( $rows->[0][0] - $rows->[1][0] ); printf("%s%.2f\n", 'Difference of $', $diff); __END__ Difference of $50.00
In reply to Re: DBI, SQL query problem
by saskaqueer
in thread DBI, SQL query problem
by sirius98
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |