Problems using count() with a join

Поиск
Список
Период
Сортировка
От Doug Younger
Тема Problems using count() with a join
Дата
Msg-id 4.2.0.58.19990818215523.00c808b0@proxy
обсуждение исходный текст
Ответы Re: [SQL] Problems using count() with a join  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-sql
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.


В списке pgsql-sql по дате отправления:

Предыдущее
От: Drew Whittle
Дата:
Сообщение: Counting the number of distinct rows returned
Следующее
От: "tjk@tksoft.com"
Дата:
Сообщение: Re: [SQL] Counting the number of distinct rows returned