X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 79C4D9FA506 for ; Mon, 11 Sep 2006 01:54:34 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 28366-03 for ; Mon, 11 Sep 2006 01:54:26 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.178]) by postgresql.org (Postfix) with ESMTP id 7BA1E9F9F80 for ; Mon, 11 Sep 2006 01:54:26 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so1994137pyd for ; Sun, 10 Sep 2006 21:54:25 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=VP/ySd5kyv8TWh7e0gGUj4ozM3yZZorWFsvAK8ak4yKSZgfvaymT5MCho27qlKFhfd1ebyqJJckn6WcrLwa4OY+P+AkA7pwW8hsP+buVIGij/gpfxpbaAs6+LgYUXb8QgEda8sy4qtJll7sE9hVuOH+5i4EbowMCOMD/ILjML4g= Received: by 10.35.93.1 with SMTP id v1mr8091595pyl; Sun, 10 Sep 2006 21:54:25 -0700 (PDT) Received: by 10.35.80.9 with HTTP; Sun, 10 Sep 2006 21:54:25 -0700 (PDT) Message-ID: Date: Sun, 10 Sep 2006 23:54:25 -0500 From: "Aaron Bono" To: "James Cloos" Subject: Re: where clause subqueries vs multiple rows results Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_66369_20445320.1157950465242" References: X-Google-Sender-Auth: 4243ed41cbcceaab X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.702 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_40_50, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/60 X-Sequence-Number: 26271 ------=_Part_66369_20445320.1157950465242 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 9/10/06, James Cloos wrote: > > I've a query which I'd have liked to word akin to: > > SELECT guid FROM child WHERE the_fkey = > ( SELECT id FROM parent WHERE name ~ 'some_regex' ) > ORDER BY the_fkey, my_pkey; > > I got around it by doing the SELECT id first, and then doing a SELECT > guid for each row returned, appending the results together. > > Can that be done in a single query, insead of 1+n queries? Will this work? SELECT guid FROM child INNER JOIN parent ON ( child.the_fkey = parent.id AND ) WHERE name ~ 'some_regex' ORDER BY child.the_fkey, child.my_pkey ; I am not sure what table name is from but since you say it is n+1 queries I assume it is from the child table? It would help to know what columns are on which tables. ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ================================================================== ------=_Part_66369_20445320.1157950465242 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 9/10/06, James Cloos <cloos@jhcloos.com> wrote:
I've a query which I'd have liked to word akin to:

  SELECT guid FROM child WHERE the_fkey =
         ( SELECT id FROM parent WHERE name ~ 'some_regex' )
         ORDER BY the_fkey, my_pkey;

I got around it by doing the SELECT id first, and then doing a SELECT
guid for each row returned, appending the results together.

Can that be done in a single query, insead of 1+n queries?

 
Will this work?

SELECT guid
FROM child
INNER JOIN parent ON (
    child.the_fkey = parent.id
    AND
)
WHERE name ~ 'some_regex'
ORDER BY
    child.the_fkey,
    child.my_pkey
;

I am not sure what table name is from but since you say it is n+1 queries I assume it is from the child table?  It would help to know what columns are on which tables.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
================================================================== ------=_Part_66369_20445320.1157950465242--