Here is an example.
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
my $dbh = DBI->connect('dbi:mysql:test','userid','password') or die "C
+annot 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 ($_) ? $_ : ' ';
print FH length ($r)>0 ? $r: ' ';
print FH "</td>";}@data;
print FH "</tr>";
}
print FH "</table>";
}
}
print FH "<p>".$ENV{"TEMP"}."\\result.html</p>\n";
print FH "</Body></HTML>\n";
close FH;
my $url = "file://".$ENV{"TMP"}."\\mytmp.html";
my $commandline = qq{start "$url" "$url"};
system($commandline) == 0
or die qq{Couldn't launch '$commandline': $!/$?};
$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;
Account | Date | Type | Total_No | Amount |
181432 | 1999-07-18 | Credit | 5 | 60000.00 |
181432 | 1999-07-11 | Debit | 6 | 83000.00 |
181432 | 1999-07-18 | Balance | 11 | -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;
Account | Date | Type | Subtype | Total_No | Amount |
181432 | 1999-07-03 | Debit | CHEQUENO | 2 | 31000.00 |
181432 | 1999-07-03 | Debit | DDNO | 2 | 28000.00 |
desc transaction;
Field | Type | Null | Key | Default | Extra |
TranID | int(10) unsigned | NO | PRI | | |
Date | date | NO | | | |
AccNum | int(10) unsigned | NO | | | |
Type | enum('Credit','Debit') | NO | | | |
Amount | decimal(10,2) | NO | | | |
ChequeNo | int(10) unsigned | YES | | | |
DDNo | int(10) unsigned | YES | | | |
select *
from animals3;
SQL Error: Table 'test2.animals3' doesn't exist
select *
from transaction;
TranID | Date | AccNum | Type | Amount | ChequeNo | DDNo |
657519 | 1999-07-02 | 181432 | Debit | 12000.00 | | |
657520 | 1999-07-02 | 181432 | Debit | 16000.00 | 465774 | |
657523 | 1999-07-02 | 181432 | Credit | 11000.00 | | |
657524 | 1999-07-02 | 181432 | Debit | 13000.00 | | 569086 |
657525 | 1999-07-03 | 181432 | Debit | 15000.00 | | 569999 |
657529 | 1999-07-03 | 181432 | Debit | 15000.00 | 466777 | |
657533 | 1999-07-05 | 181432 | Credit | 12500.00 | | |
657538 | 1999-07-09 | 181432 | Credit | 11000.00 | | |
657539 | 1999-07-10 | 181432 | Credit | 10000.00 | | |
657541 | 1999-07-11 | 181432 | Debit | 12000.00 | | |
657548 | 1999-07-18 | 181432 | Credit | 15500.00 | | |
call test.coldif("test.animals","test2.animals")
id | name | category | idp | Fecha | Cost |
1 | dog | home pet != pet | 1 | 2009-01-11 | |
2 | cat | home pet | 2 | 2009-05-06 | 000222.23 != 300.00 |
4 | lax != lax2 | wile | 1 | 2009-09-01 | 000123.44 |
5 | whale | fish1 != fish | 2 | 2009-11-12 | 000100.00 != Null |
9 | test1 != test2 | test1 != test2 | 1 | 2008-01-01 | 000150.10 |
12 != Null | testNull1 != Null | TesttNull != Null | 5 != Null | | |
18 != Null | castor != Null | Wiled != Null | 7 != Null | 2010-09-15 != Null | 000001.01 != Null |
Null != 11 | Null != testNull2 | Null != TesttNull | Null != 5 | | |
C:\Users\GMONTE~1\AppData\Local\Temp\result.html
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.