Re: Stored Procedure Assistance

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: "Bradley J(dot) Bartram" <bbartram(at)dyrectmedia(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Stored Procedure Assistance
Date: 2003-07-08 17:15:45
Message-ID: 20030708171545.GB12195@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jul 08, 2003 at 12:55:00PM -0400, Bradley J. Bartram wrote:

> SELECT c FROM table_b WHERE column_b = a
>
> The next query has some logic in php that constructs it. Basically if c > 0
> than the results of query 2 are setup as ORs in the WHERE clause.
>
> SELECT d FROM table_c WHERE column_c = c1 OR column_c = c2, etc.
>
> The first two queries are not a problem, but how can I take the array of
> results from query 2 and put them into query 3?

Huh... why don't you do a

SELECT d FROM table_c WHERE column_c IN (SELECT c FROM table_b WHERE column_b = a)
(or convert to EXISTS for performance)

Another approach could be to build a string, say

string = ''SELECT d FROM table_c WHERE '';
FOR blah IN SELECT-values-for-c LOOP
string = string || ''OR column_c = '' || blah
END LOOP;
EXECUTE string;

(adjust as needed, exclude the OR in the first iteration, etc)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Los romanticos son seres que mueren de deseos de vida"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2003-07-08 17:21:42 Re: SQL Functions and plan time
Previous Message Maksim Likharev 2003-07-08 17:14:55 Re: PG crash on simple query, story continues