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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |