Re: count() and multiple tables - Mailing list pgsql-sql

From Josh Berkus
Subject Re: count() and multiple tables
Date
Msg-id web-26888@davinci.ethosmedia.com
Whole thread Raw
In response to count() and multiple tables  (Joseph Shraibman <jks@selectacast.net>)
List 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
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Manual Trigger Creation
Next
From: Joseph Shraibman
Date:
Subject: Re: Select very slow...