Re: Help with multistage query

From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Matt Emmerton <matt(at)gsicomp(dot)on(dot)ca>
Cc: Russell Simpkins <russellsimpkins(at)hotmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with multistage query
Date: 2005-10-04 22:34:50
Message-ID: 20051004223450.GM40138@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Sep 07, 2005 at 05:37:47PM -0400, Matt Emmerton wrote:
>
> ----- Original Message -----
> From: Russell Simpkins
> To: pgsql-sql(at)postgresql(dot)org
> Sent: Wednesday, September 07, 2005 4:05 PM
> Subject: Re: [SQL] Help with multistage query
>
> I have a perl script that issues a series of SQL statements to perform some queries. The script works, but I believe there must be a more elegant way to do this.
>
>
>
> The simplified queries look like this:
>
>
>
> SELECT id FROM t1 WHERE condition1; ;returns about 2k records which are stored in @idarray
>
>
>
> foreach $id (@idarray) {
>
> SELECT x FROM t2 WHERE id=$id; ; each select returns about 100 records which are saved in a perl variable
>
> }
>
> how about
> select t1.id from t1, t2 where t1.id = t2.id and t2.id = x
>
> or more correctly, based on the OP's example:
>
> select t2.x from t1, t2 where t1.id = t2.id and t1.id = <condition>

Actually, I think you want AND t2.x <condition>, not t1.id.

BTW, I recommend not using id as a bareword field name. Very easy to get
confused when you start joining a bunch of stuff together.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jim C. Nasby 2005-10-04 22:52:50 Re: Why doesn't the SERIAL data type automatically have a
Previous Message Gregory S. Williamson 2005-10-04 22:33:28 Re: using pg_tables and tablename in queries