Hey,

To select the individual groups only once, you can also use the DISTINCT clause for the select statement.

Then you can use the results in a prepared cache to find the results for each individual group. monarch seems to have a correct solution also. But I'd thought I post something about the DISTINCT clause because it solves your first problem really well.
my $dbh = DBI->connect('DBI:mysql:DB', 'user_n', $pass) or die "Failed + connecting to DB " . DBI->errstr; my ($groups, $results); my $sth = $dbh->prepare_cached(<<SQL); SELECT DISTINCT group from table SQL $sth->execute or die "executing: ", $dbh->errstr;; $sth->bind_columns(undef, \$groups); #bind the results--only contains unique group results. my $sth_x = $dbh->prepare_cached(<<SQL); SELECT somedata FROM table where group = ? SQL my $found = 0; my $found_x = 0; print "<b>$groups</b><hr>"; while ($sth->fetch) { $found++; $sth_x->execute($groups) or die "executing: ", $dbh->errstr; #find results using the results from the first query $sth_x->bind_columns(undef, \$results); while($sth_x->fetch) { $found_x++; print "$results<br>"; } unless($found_x) { print "empty for this group"; } print "<br>"; my $found_x = 0; #reset 2nd search found for next group result query. } unless ($found) { print "no groups matched"; }
perleager

In reply to Re: mysql sorting by perleager
in thread mysql sorting by sulfericacid

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.