I'm gonna assume you're using MySQL 3.x, which I think doesn't have subqueries.

my $q_state = $dbh->quote($state); my $q_status = $dbh->quote($status); my $q_comment_status = $dbh->quote($comment_status); my $stmt1_where = ""; $stmt1_where .= " AND Speedtrap.state=$q_state\n" if ($state && $state ne 'all'); $stmt1_where .= " AND Speedtrap.approval=$q_status\n" if ($status && $status ne 'all'); $stmt1_where .= " AND Comments.approval=$q_comment_status\n" if ($comment_status && $comment_status ne 'all'); $stmt1_where .= "\n"; my $stmt1 = <<";"; SELECT DISTINCT Speedtrap.id, Speedtrap.state, Speedtrap.city, Speedtrap.discription, Speedtrap.approval, FROM Speedtrap LEFT JOIN Comments ON Speedtrap.id = Comments.id WHERE 1=1 $stmt1_where ORDER BY $sort $sortord ; my $stmt2_where = ""; $stmt2_where .= " AND Comments.approval=$q_comment_status\n" if ($comment_status && $comment_status ne 'all'); $stmt2_where .= "\n"; my $stmt2 = <<";"; SELECT COUNT(*) AS num_comments FROM Comments WHERE Comments.id = ? $stmt2_where ORDER BY $sort $sortord ; my $sth1 = $dbh->prepare($stmt1) || die("...: $DBI::errstr\n"); my $sth2 = $dbh->prepare($stmt2); || die("...: $DBI::errstr\n"); $sth1->execute() or die("...: $DBI::errstr\n"); ...[ print start of page ]... while (@row = $sth1->fetchrow_array()) { my ($id, $state, $city, $desc, $approval) = @row; $sth2->execute($id) or die("...: $DBI::errstr\n"); # Should always return exactly one record. my ($comment_count) = $sth2->fetchrow_array(); ...[ print repeated part ]... } ...[ print bottom of page ]... $sth2->finish(); $sth1->finish();

In reply to Re^3: Double Database Search and Sort by ikegami
in thread Double Database Search and Sort by awohld

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.