in reply to How to add MySql Records To Array or Hash?

This example will execute all the SQL stored in the @SQL array en generate a temp file with HTML code with the results.

#!/usr/bin/perl use DBI; use strict; use warnings; my $dbh = DBI->connect('dbi:mysql:test2','user','password') or die "Ca +nnot connect: $DBI::errstr\n"; my @sql =(); $sql[0] = qq[SELECT AccNum as Account, max(date) as Date, Type, count( +*) as Total_No, sum(Amount) as Amount FROM transaction GROUP BY AccNum, Type union SELECT AccNum, max(date),'Balance', count(*), sum(if(type='Debit',-1*A +mount,Amount)) FROM transaction GROUP BY AccNum;]; $sql[1] = qq[SELECT AccNum as Account, max(date) as Date, Type, 'CHEQU +ENO' as Subtype, count(*) as Total_No, sum(Amount) as Amount FROM transaction where chequeno is not null GROUP BY AccNum, Type union SELECT AccNum as Account, max(date) as Date, Type, 'DDNO' as Subtype, +count(*) as Total_No, sum(Amount) as Amount FROM transaction where DDNO is not null GROUP BY AccNum, Type;]; $sql[2] = qq[desc transaction;]; $sql[3] = qq[select * from animals3;]; $sql[4] = qq[select * from transaction;]; $sql[5] = qq[call test.coldif("test.animals","test2.animals")]; open (FH, ">".$ENV{"TMP"}."\\mytmp.html") || die "Cannot open temporar +y file: $!\n"; print FH '<HTML><Body>'; foreach (@sql) { my $l = $_; $l =~ s/\n/<br>/g; print FH '<p>'.$l.'</p>'; my $tran_cur = $dbh->prepare($_)or print FH '<p><font color="red"> +Cannot prepare: '."$DBI::errstr</font></p>"; $tran_cur->execute or print FH '<p><font color="red">SQL Error: '. +"$DBI::errstr</font></p>"; unless(defined $DBI::err){ print FH '<table border=1 cellspacing=1 cellpadding=2><tr>'; map {print FH "<td>$_</td>"}@{$tran_cur->{NAME}}; print FH "</tr><br/>"; while( my @data = $tran_cur->fetchrow_array) { print FH "<tr>"; map {print FH "<td>"; my $r = defined ($_) ? $_ : '&nbsp;'; print FH length ($r)>0 ? $r: '&nbsp;'; print FH "</td>";}@data; print FH "</tr>"; } print FH "</table>"; } } print FH "<p>".$ENV{"TEMP"}."\\result.html</p>\n"; my $url = "file://".$ENV{"TMP"}."\\mytmp.html"; my $commandline = qq{start "$url" "$url"}; system($commandline) == 0 or die qq{Couldn't launch '$commandline': $!/$?}; print FH "</Body></HTML>\n"; close FH; $dbh->disconnect; sleep(5); unlink $ENV{"TMP"}."\\mytmp.html" or warn "Could not unlink ".$ENV{"TM +P"}."\\mytmp.html: $!"; #print $ENV{"TEMP"}."\\result.html"; __DATA__ SELECT AccNum, Type, SUM(Amount) FROM transaction GROUP BY AccNum, Typ +e; SELECT AccNum, count(*), max(date), Type, sum(Amount) FROM transaction + GROUP BY AccNum, Type union SELECT AccNum, max(date),'Balance', count(*), sum(if(type='Debit',-1*A +mount,Amount)) FROM transaction GROUP BY AccNum; SELECT * FROM transaction t where chequeno is not null or ddno is not +null; SELECT AccNum, Type, sum(Amount) FROM transaction e GROUP BY AccNum, T +ype; DROP TABLE IF EXISTS test2.transaction; CREATE TABLE test2.transaction ( TranID int(10) unsigned NOT NULL, Date date NOT NULL, AccNum int(10) unsigned NOT NULL, Type enum('Credit','Debit') NOT NULL, Amount decimal(10,2) NOT NULL, ChequeNo int(10) unsigned DEFAULT NULL, DDNo int(10) unsigned DEFAULT NULL, PRIMARY KEY (TranID) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; insert into entries values(TranID,Date,AccNum,Type,Amount,ChequeNo,DDN +o'); insert into entries values(657520,'1999-07-02',0181432,'Debit',16000,4 +65774,null); insert into entries values(657524,'1999-07-02',0181432,'Debit',13000,n +ull,569086); insert into entries values(657538,'1999-07-09',0181432,'Credit',11000, +null,null); insert into entries values(657548,'1999-07-18',0181432,'Credit',15500, +null,null); insert into entries values(657519,'1999-07-02',0181432,'Debit',12000,n +ull,null); insert into entries values(657523,'1999-07-02',0181432,'Credit',11000, +null,null); insert into entries values(657529,'1999-07-03',0181432,'Debit',15000,4 +66777,null); insert into entries values(657539,'1999-07-10',0181432,'Credit',10000, +null,null); insert into entries values(657541,'1999-07-11',0181432,'Debit',12000,n +ull,null); insert into entries values(657525,'1999-07-03',0181432,'Debit',15000,n +ull,569999); insert into entries values(657533,'1999-07-05',0181432,'Credit',12500, +null,null);

Result

SELECT AccNum as Account, max(date) as Date, Type, count(*) as Total_No, sum(Amount) as Amount
FROM transaction GROUP BY AccNum, Type
union
SELECT AccNum, max(date),'Balance', count(*), sum(if(type='Debit',-1*Amount,Amount))
FROM transaction GROUP BY AccNum;


AccountDateTypeTotal_NoAmount
1814321999-07-18Credit560000.00
1814321999-07-11Debit683000.00
1814321999-07-18Balance11-23000.00

SELECT AccNum as Account, max(date) as Date, Type, 'CHEQUENO' as Subtype, count(*) as Total_No, sum(Amount) as Amount
FROM transaction where chequeno is not null GROUP BY AccNum, Type
union
SELECT AccNum as Account, max(date) as Date, Type, 'DDNO' as Subtype, count(*) as Total_No, sum(Amount) as Amount
FROM transaction where DDNO is not null GROUP BY AccNum, Type;


AccountDateTypeSubtypeTotal_NoAmount
1814321999-07-03DebitCHEQUENO231000.00
1814321999-07-03DebitDDNO228000.00

desc transaction;


FieldTypeNullKeyDefaultExtra
TranIDint(10) unsignedNOPRI  
DatedateNO   
AccNumint(10) unsignedNO   
Typeenum('Credit','Debit')NO   
Amountdecimal(10,2)NO   
ChequeNoint(10) unsignedYES   
DDNoint(10) unsignedYES   

select *
from animals3;

SQL Error: Table 'test2.animals3' doesn't exist

select *
from transaction;


TranIDDateAccNumTypeAmountChequeNoDDNo
6575191999-07-02181432Debit12000.00  
6575201999-07-02181432Debit16000.00465774 
6575231999-07-02181432Credit11000.00  
6575241999-07-02181432Debit13000.00 569086
6575251999-07-03181432Debit15000.00 569999
6575291999-07-03181432Debit15000.00466777 
6575331999-07-05181432Credit12500.00  
6575381999-07-09181432Credit11000.00  
6575391999-07-10181432Credit10000.00  
6575411999-07-11181432Debit12000.00  
6575481999-07-18181432Credit15500.00  

call test.coldif("test.animals","test2.animals")


idnamecategoryidpFechaCost
1doghome pet != pet12009-01-11 
2cathome pet22009-05-06000222.23 != 300.00
4lax != lax2wile12009-09-01000123.44
5whalefish1 != fish22009-11-12000100.00 != Null
9test1 != test2test1 != test212008-01-01000150.10
12 != NulltestNull1 != NullTesttNull != Null5 != Null  
18 != Nullcastor != NullWiled != Null7 != Null2010-09-15 != Null000001.01 != Null
Null != 11Null != testNull2Null != TesttNullNull != 5  

C:\Users\GMONTE~1\AppData\Local\Temp\result.html

Replies are listed 'Best First'.
Re^2: How to add MySql Records To Array or Hash?
by Anonymous Monk on Sep 09, 2013 at 21:33 UTC