pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Doug Younger <postgres@mindspring.com>
To: pgsql-sql@postgresql.org
Subject: Problems using count() with a join
Date: Wed, 18 Aug 1999 22:10:35 -0400
Message-ID: <4.2.0.58.19990818215523.00c808b0@proxy> (raw)

Hi,
   I have the following 2 tables:

Table groups:
   g_id     int4
   g_name   text

Table users:
   u_id     int4
   g_id     int4

What I want is to get a count of # of users in each group, even if there 
are no users in the group.

I have tried the following:
SELECT t1.g_name,count(t2.g_id)
   FROM groups t1,users t2
   WHERE t1.g_id = t2.g_id
   GROUP BY t1.g_name;

But it only give the counts of users in each group if there actually are 
users in that group.
I want the count to be 0 if there are no users in the group.

Thanks.



view thread (2+ messages)  latest in thread

Message-ID: <4.2.0.58.19990818215523.00c808b0@proxy>
Permalink:  ../4.2.0.58.19990818215523.00c808b0@proxy/
Also on:    postgresql.org/message-id/4.2.0.58.19990818215523.00c808b0@proxy

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: postgres@mindspring.com
  Subject: Re: Problems using count() with a join
  In-Reply-To: <4.2.0.58.19990818215523.00c808b0@proxy>
  References: <4.2.0.58.19990818215523.00c808b0@proxy>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox