finhagen:

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.


In reply to Re: mysql DBI Nested Queries by roboticus
in thread mysql DBI Nested Queries by finhagen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.