Re: count() and multiple tables

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Joseph Shraibman <jks(at)selectacast(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: count() and multiple tables
Date: 2001-03-19 23:52:27
Message-ID: web-26888@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Joseph,

SImple as pie (e.g., easy on your 100th one):

> select count(*) from d where status = 2 and d.id = u.dkey and
> u.status =
> 2 and not u.b and u.akey = a.key and a.status = 3;

Count(*) will always count the number of records in the largest table in
your join. If you want the number of records in a specific table, then
you have to make sure that that table is the only on in your FROM
clause, and reference all other tables in WHERE:

select count(*) from d
where id IN (select u.dkey from u, a
where u.akey = a.key
AND u.status = 2 and not u.b
and a.status = 3);

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joseph Shraibman 2001-03-19 23:54:06 Re: Select very slow...
Previous Message Stephan Szabo 2001-03-19 23:52:12 Re: count() and multiple tables