Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 7934D1337B67 for ; Tue, 12 Apr 2011 10:42:18 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 15551-06 for ; Tue, 12 Apr 2011 13:42:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gw.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 3A70A1337B47 for ; Tue, 12 Apr 2011 10:42:11 -0300 (ADT) Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Tue, 12 Apr 2011 08:42:10 -0500 Message-Id: <4DA41058020000250003C6C9@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Tue, 12 Apr 2011 08:42:00 -0500 From: "Kevin Grittner" To: ,"Paul" Subject: Re: BUG #5972: Update with subquery: erroneous results for foreign key field References: <201104120859.p3C8xtgF074076@wwwmaster.postgresql.org> In-Reply-To: <201104120859.p3C8xtgF074076@wwwmaster.postgresql.org> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/58 X-Sequence-Number: 29977 "Paul" wrote: > update cart set status = 1 where userid = (select userid from > users where email = 'example@example.com'); > > As you can see, the subquery is broken (users table doesn't have > the column userid). By standard, if the identifier isn't defined within the most local scope, each enclosing scope, from the inside out, will be checked. I would expect the above to update each row where cart.userid was not null. I always use and recommend aliases where practical. If you wrote it this way, such a mistake would be clearly identified: update cart set status = 1 where userid = (select u.userid from users u where u.email = 'example@example.com'); Not a bug. -Kevin