X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AE739DA0DE for ; Tue, 4 Oct 2005 19:34:52 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 04829-05 for ; Tue, 4 Oct 2005 22:34:47 +0000 (GMT) Received: from flake.decibel.org (flake.decibel.org [67.100.216.10]) by svr1.postgresql.org (Postfix) with ESMTP id A5A2BDA0A4 for ; Tue, 4 Oct 2005 19:34:48 -0300 (ADT) Received: by flake.decibel.org (Postfix, from userid 1001) id 813481525C; Tue, 4 Oct 2005 17:34:50 -0500 (CDT) Date: Tue, 4 Oct 2005 17:34:50 -0500 From: "Jim C. Nasby" To: Matt Emmerton Cc: Russell Simpkins , pgsql-sql@postgresql.org Subject: Re: Help with multistage query Message-ID: <20051004223450.GM40138@pervasive.com> References: <000001c5b3d5$a67a3540$0300a8c0@dell8200> <003001c5b3f4$6a6911b0$1200a8c0@gsicomp.on.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <003001c5b3f4$6a6911b0$1200a8c0@gsicomp.on.ca> X-Operating-System: FreeBSD 4.11-RELEASE-p10 i386 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 required=5 tests=[AWL=0.008] X-Spam-Level: X-Archive-Number: 200510/12 X-Sequence-Number: 22865 On Wed, Sep 07, 2005 at 05:37:47PM -0400, Matt Emmerton wrote: > > ----- Original Message ----- > From: Russell Simpkins > To: pgsql-sql@postgresql.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 = Actually, I think you want AND t2.x , 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@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461