Re: Stored Procedure Assistance - Mailing list pgsql-general

From Richard Huxton
Subject Re: Stored Procedure Assistance
Date
Msg-id 200307081827.12833.dev@archonet.com
Whole thread Raw
In response to Stored Procedure Assistance  ("Bradley J. Bartram" <bbartram@dyrectmedia.com>)
List pgsql-general
On Tuesday 08 Jul 2003 5:55 pm, Bradley J. Bartram wrote:
> The first query is simple:
>
> SELECT a FROM table_a WHERE column_a = b
>
> This will return a single row.  The next query takes that derived value and
> does a simliar select that returns multiple rows.
>
> 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?

Well, we can combine (1) and (2):

SELECT c FROM table_b, table_a
WHERE table_b.column_b = table_a.a
AND table_a.column_a = b;

The third needs to be something like:

SELECT d FROM table_c WHERE column_c IN (...the combined select above...)

So long as you don't have too many results this should work fine - no need for
a stored procedure at all.

If I've understood your problem, that should work.
--
  Richard Huxton

pgsql-general by date:

Previous
From: Chris Travers
Date:
Subject: Re: Benchmarking
Next
From: Dragan Matic
Date:
Subject: Is Postgres broken in Red Hat 9?