Re: joining a query with a select count(*)

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Matthew Terenzio <matt(at)jobsforge(dot)com>
Cc: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: joining a query with a select count(*)
Date: 2005-11-05 18:47:31
Message-ID: 20051105184725.GB6828@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Nov 05, 2005 at 01:10:13PM -0500, Matthew Terenzio wrote:
>
> I want a query to return all the rows from one table along with a count
> of rows in another table that reference each returned row from the
> first table.

Quick and dirty, should give you the idea...

select *, (select count(*) from table2 where fkey=pkey) from table1;

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-11-05 19:08:49 Re: Missing variable "role" in "pg_settings"?
Previous Message Matthew Terenzio 2005-11-05 18:10:13 joining a query with a select count(*)