Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id DEFA81337B5E for ; Mon, 29 Nov 2010 05:45:10 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 08525-02 for ; Mon, 29 Nov 2010 09:45:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [67.192.120.76]) by mail.postgresql.org (Postfix) with ESMTP id 625251337B58 for ; Mon, 29 Nov 2010 05:45:02 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=nxyhu1BW89F8znyKlGn03VG5CIMvELf5dQuByNrGFY8=; b=dQlvytafO37TNq/YGuxwSKnXfjmFmVcXbW8PMYDQpcth1CgC11fLn1Ng/Ex2Wep9SXL3m1JL6sCxwxfe2W0BcqlHSbpsj8gmp6v+364yoPimiDhhoJUzwRahXvEOh8h+; Received: from ip-118-90-139-227.xdsl.xnet.co.nz ([118.90.139.227] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1PN0I8-0006RU-Jh; mid 144 for pgsql-sql@postgresql.org; Mon, 29 Nov 2010 09:45:01 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1PN0Hs-0002NZ-Mv for pgsql-sql@postgresql.org; Mon, 29 Nov 2010 22:44:44 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: subselect and left join not working? Date: 29 Nov 2010 09:44:44 GMT Organization: Dis (not Dat) Organisation Lines: 31 Message-ID: References: <749730.36457.qm@web113802.mail.gq1.yahoo.com> NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1291023884 6945 127.0.0.1 (29 Nov 2010 09:44:44 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Smtpcorp-Track: 17070063.1.59196409 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.801 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_SORBS_DUL=0.001 X-Spam-Level: X-Archive-Number: 201011/81 X-Sequence-Number: 34819 On 2010-11-29, Jorge Arenas wrote: > select zona_id from zonas where zona_id not in (select zona_id from usuarios ####### ####### ####### > where per_id =2) select 'FRED' from from usuarios where per_id =2 what'shappening is your not in subquery is being 'corrupted' by the surrounding query, the expression zona_id is being replaced with the value from the main query. so the inner query return multiple copies of the value from the outer query and the not-in fails. to avoid that confusion do it this way: select zona_id as z from zonas where z not in (select zona_id from usuarios where per_id =2) or this way: select zona_id from zonas where zona_id not in (select usuarios.zona_id from usuarios where per_id =2) or this way: select zona_id from zonas where zona_id not in (select u.zona_id from usuarios as u where per_id =2) -- ⚂⚃ 100% natural