Skip site navigation (1) Skip section navigation (2)

Re: find the number of rows for each table

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Patrick Coulombe <pcoulombe(at)mediacces(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: find the number of rows for each table
Date: 2000-06-18 14:16:26
Message-ID: Pine.LNX.4.21.0006172011290.348-100000@localhost.localdomain (view raw or flat)
Thread:
Lists: pgsql-sql
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(at)gmx(dot)net                   75262 Uppsala
http://yi.org/peter-e/            Sweden


In response to

pgsql-sql by date

Next:From: Peter EisentrautDate: 2000-06-18 14:24:43
Subject: Re: Database Transfer
Previous:From: Craig MayDate: 2000-06-17 23:07:07
Subject: Database Transfer

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group