Received: from merganser.its.uu.se (merganser.its.uu.se [130.238.6.236]) by hub.org (8.10.1/8.10.1) with ESMTP id e5IEALM96566 for ; Sun, 18 Jun 2000 10:10:21 -0400 (EDT) Received: from regulus.student.UU.SE ([130.238.5.2]:52603 "EHLO regulus.its.uu.se") by merganser.its.uu.se with ESMTP id ; Sun, 18 Jun 2000 16:09:53 +0200 Received: from peter (helo=localhost) by regulus.its.uu.se with local-esmtp (Exim 3.02 #2) id 133fsA-0000AC-00; Sun, 18 Jun 2000 16:16:26 +0200 Date: Sun, 18 Jun 2000 16:16:26 +0200 (CEST) From: Peter Eisentraut To: Patrick Coulombe cc: pgsql-sql@postgresql.org Subject: Re: find the number of rows for each table In-Reply-To: <20000615.20103600@modemcable079.96-200-24.mtl.mc.videotron.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=ISO-8859-1 Content-Transfer-Encoding: 8BIT X-Archive-Number: 200006/171 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