in reply to mysql DBI Nested Queries
If you're doing a report of employees by supervisor, or similar, another way to handle the project is to make the database do some of the work for you. For example, you might be able to make a query give you the information sorted conveniently, and then add a control break. Perhaps something like:
our $lname; my $sth = $dbh->prepare(<<EOSQL); SELECT sup.fname, sup.lname, sup.title, sup.supervisor, emp.fname, emp.lname, emp.title, emp.supervisor FROM employeedb sup JOIN employeedb emp ON ..... ORDER BY sup.fname, sup.lname, emp.fname, emp.lname EOSQL $sth->execute or die $sth->errstr; my $previous_supervisor = ''; while (my $aref = $sth->fetchrow_arrayref) { my ($sup_fname, $sup_lname, $sup_title, $emp_fname, $emp_lname, $emp_title) = @$aref; # control-break: if a new supervisor is found, skip a few lines and + add a header my $current_supervisor = "$sup_fname $sup_lname"; if ($current_supervisor ne $previous_supervisor) { $R += 3; $C = 0; $worksheet0->write($R, $C++, "SUPERVISOR: ", $format_HEADER); $worksheet0->write($R, $C++, $current_supervisor, $format_HEADER +); $R++; $C=0; $worksheet0->write($R, $C++, "First Name", $format_COL_HEADER); $worksheet0->write($R, $C++, "Last Name", $format_COL_HEADER); $worksheet0->write($R, $C++, "Title", $format_COL_HEADER); $worksheet0->write($R, $C++, "Supervisor", $format_COL_HEADER); } # Add employee to sheet $R++; $C=0; $worksheet0->write($R, $C++, $emp_fname, $format_NORMAL); $worksheet0->write($R, $C++, $emp_lname, $format_NORMAL); $worksheet0->write($R, $C++, $emp_title, $format_NORMAL); $worksheet0->write($R, $C++, $emp_supervisor, $format_NORMAL); }
(Yes, I know this doesn't match your task exactly, it's just an example you might be able to adapt.)
Your example shows nearly duplicated code, which indicates that you might be able to restructure your code to use a subroutine:
sub process_employee { my ($worksheet0, $employee, $format, $col, $row) = @_; my $sth = $dbh->prepare("SELECT fname, lname, title, supervisor " ."FROM employeedb WHERE eid = ?"); $sth->execute($employee) or die $sth->errstr; my ($fname, $lname, $title, $supervisor); while (my $aref = $sth ->fetchrow_arrayref) { ($fname, $lname, $title, $supervisor) = @$aref; $worksheet0->write($row++, $col, $fname, $format); $worksheet0->write($row++, $col, $lname, $format); $worksheet0->write($row++, $col, $title, $format); $worksheet0->write($row++, $col, $supervisor, $format); } return $supervisor } my $supervisor = process_employee($worksheet0, $employee1, $format_PRD +middle, 19, 26); if (defined $supervisor) { process_employee($worksheet0, $supervisor, $format_AMCtop, 23, 11) +; }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: mysql DBI Nested Queries
by finhagen (Sexton) on Dec 23, 2012 at 22:40 UTC | |
by roboticus (Chancellor) on Dec 23, 2012 at 23:55 UTC | |
by finhagen (Sexton) on Dec 26, 2012 at 23:34 UTC | |
by finhagen (Sexton) on Dec 28, 2012 at 14:47 UTC | |
by roboticus (Chancellor) on Dec 28, 2012 at 15:50 UTC |