pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: Patrick Coulombe <pcoulombe@mediacces.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: find the number of rows for each table
Date: Sun, 18 Jun 2000 16:16:26 +0200 (CEST)
Message-ID: <Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain> (raw)
In-Reply-To: <20000615.20103600@modemcable079.96-200-24.mtl.mc.videotron.net>

Patrick Coulombe writes:

> I want to find the number of rows for each table (2) in 1 query,
> the query is:

To find the number of rows in one table you do

SELECT COUNT(*) FROM table;

To find the number of rows in two tables you send two queries like
that. There's no good way to do it in one shot, nor is there any reason
why there should: if you want two different pieces of information you send
two queries. Also note that some of your attempts are going to be wildly
slower than just two straightforward count(*) queries.

> 
> select distinct * from medias, contacts WHERE medias.media_id = 
> contacts.media_id AND medias_categories.media_id = medias.media_id AND 
> medias_categories.categorie_id = 1
> --------
> 700 rows
> 
> 
> If I want to find the number of row for medias (for the same query) I 
> have to do a other query :
> 
> select distinct * from medias WHERE medias.media_id = 
> contacts.media_id AND medias_categories.media_id = medias.media_id AND 
> medias_categories.categorie_id = 1
> --------
> 60 rows
> 
> 
> I search for a solution, I try that :
> 
> select distinct *, count(distinct (medias.media_id)), count(distinct 
> (contacts.contact_id)) from medias, contacts WHERE medias.media_id = 
> contacts.media_id AND medias_categories.media_id = medias.media_id AND 
> medias_categories.categorie_id = 1
> -------------------------------------------------
> ERROR:  parser: parse error at or near "distinct"
> 
> don't work, as you can see.

-- 
Peter Eisentraut                  Sernanders väg 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden




view thread (2+ messages)

Message-ID: <Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain>
Permalink:  ../Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain/
Also on:    postgresql.org/message-id/Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain

 · 

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: peter_e@gmx.net, pcoulombe@mediacces.com
  Subject: Re: find the number of rows for each table
  In-Reply-To: <Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain>
  References: <20000615.20103600@modemcable079.96-200-24.mtl.mc.videotron.net> <Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain>

* 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