Re: sub-select parameter problem - Mailing list pgsql-sql

From Richard Huxton
Subject Re: sub-select parameter problem
Date
Msg-id 200403041151.21946.dev@archonet.com
Whole thread Raw
In response to sub-select parameter problem  ("Philippe Lang" <philippe.lang@attiksystem.ch>)
List pgsql-sql
On Thursday 04 March 2004 11:20, Philippe Lang wrote:
[working query]

> It works fine.

Excellent! :-)
Oh - there's more :-(

> Now, I need to do something else: the parameter of my sub-select is also
> a member of the table I'm selecting.
>
> SELECT
>
>   tableA.field1,
>   tableA.field2,
>
>   tableB.field1,
>   tableB.field2,
>
>   (
>     SELECT tableB.field2
>     FROM tableB
>     WHERE tableB.field1 = tableB.field1 (--> from-main-select?) - 1;
>   ) AS p
>
> FROM tableA
> INNER JOIN tableB
> ON tableA.pk = tableB.FK;
> -------------------------------------------
>
> How can I refer to the tableB.field1 parameter from the main query? I've
> tried to do something like this, but without success:

You're along the right lines, except I think you want to alias the table not 
the column.

...
( SELECT x.field2 FROM tableB AS x WHERE x.field1 = tableB.field1 - 1
)
...

HTH
--  Richard Huxton Archonet Ltd


pgsql-sql by date:

Previous
From: "Philippe Lang"
Date:
Subject: sub-select parameter problem
Next
From: Christoph Haller
Date:
Subject: Re: sub-select parameter problem