I'd probably use this approach:
SELECT tablea.id, COUNT(*) FROM tablea, tableb WHERE tablea.foo = tableb.bar GROUP BY tablea.id
Here the id column is some column that identifies the row. It could be the foo column or some other column or even a collection of columns. Whatever it is, just put it in the SELECT phrase in addition to the GROUP BY phrase, e.g. if your rows in tablea are identified by the columns id1 and id2, then use:
SELECT tablea.id1, tablea.id2, COUNT(*) FROM ... ... GROUP BY id1, id2
The sub-select approach will work, but databases have a lot of techniques for efficiently executing GROUP BY queries. With the sub-select approach you run the risk of explicitly telling the database how to run the query which doesn't allow it to optimize it.

In reply to Re: perl dbi select question - using GROUP BY by pc88mxer
in thread perl dbi select question by baxy77bax

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.