Re: Function as Phantom Field - Mailing list pgsql-general

From tolik@aaanet.ru (Anatoly K. Lasareff)
Subject Re: Function as Phantom Field
Date
Msg-id 868zrt3wkz.fsf@tolikus.hq.aaanet.ru
Whole thread Raw
In response to Function as Phantom Field  (Charles Tassell <ctassell@isn.net>)
List pgsql-general
>>>>> "CT" == Charles Tassell <ctassell@isn.net> writes:

 CT> I have a database like this:
 CT> CREATE TABLE articles (
 CT> article_id      serial primary key,
 CT> title           text
 CT> );

 CT> CREATE TABLE pages (
 CT> article_id      integer,
 CT> page            text
 CT> );

 CT> And I want to be able to do a

 CT> SELECT article_id, title, count_pages(article_id) FROM articles

 CT> Now, I have already written the count_pages function (it's just a count(*)
 CT> where article_id = $1) but I don't know how to do the SELECT in one pass,
 CT> is there some way I can do this with triggers or is there a special field
 CT> name I can use to specify the current article_id as the function argument?

Try this:

select articles.article_id, count(*) from articles, pages where
article.article_id = pages.article_id group by pages.article_id;

--
Anatoly K. Lasareff              Email:       tolik@aaanet.ru

pgsql-general by date:

Previous
From: Henk van Lingen
Date:
Subject: Re: cache lookup failed
Next
From: Michael Ansley
Date:
Subject: RE: ODMG