pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Philippe Lang <philippe.lang@attiksystem.ch>
To: pgsql-sql@postgresql.org
Subject: Re: sub-select parameter problem
Date: Thu, 4 Mar 2004 11:51:21 +0000
Message-ID: <200403041151.21946.dev@archonet.com> (raw)
In-Reply-To: <6C0CF58A187DA5479245E0830AF84F42080222@poweredge.attiksystem.ch>
References: <6C0CF58A187DA5479245E0830AF84F42080222@poweredge.attiksystem.ch>

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



view thread (3+ messages)  latest in thread

Message-ID: <200403041151.21946.dev@archonet.com>
Permalink:  ../200403041151.21946.dev@archonet.com/
Also on:    postgresql.org/message-id/200403041151.21946.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, philippe.lang@attiksystem.ch
  Subject: Re: sub-select parameter problem
  In-Reply-To: <200403041151.21946.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox