Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1E5584770B6 for ; Tue, 28 Jan 2003 11:05:46 -0500 (EST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 6C8EA476070 for ; Tue, 28 Jan 2003 10:56:27 -0500 (EST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.6/8.12.6) with ESMTP id h0SFuT5u013473; Tue, 28 Jan 2003 10:56:29 -0500 (EST) To: Michael Hostbaek Cc: pgsql-sql@postgresql.org Subject: Re: subselect return in where clause In-reply-to: <20030128112123.GC67742@mich2.itxmarket.com> References: <20030128112123.GC67742@mich2.itxmarket.com> Comments: In-reply-to Michael Hostbaek message dated "Tue, 28 Jan 2003 12:21:23 +0100" Date: Tue, 28 Jan 2003 10:56:28 -0500 Message-ID: <13472.1043769388@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/440 X-Sequence-Number: 11305 Michael Hostbaek writes: > select some_stuff, more_stuff, > (select other_stuff from other_table where other_table.stuff ilike > stuff.match) as other_stuff from stuff where other_stuff = 'hello' The above is nonsensical: WHERE cannot refer to values computed in the SELECT list, because WHERE logically occurs before the SELECT list is executed (indeed, the SELECT list will not be executed at all, if WHERE returns false). You could repeat yourself: select some_stuff, more_stuff, (select other_stuff from other_table where other_table.stuff ilike stuff.match) as other_stuff from stuff where (select other_stuff from other_table where other_table.stuff ilike stuff.match) = 'hello' or if it really bugs you to write the expression twice, perhaps use a sub-select: select * from (select some_stuff, more_stuff, (select other_stuff from other_table where other_table.stuff ilike stuff.match) as other_stuff from stuff) as subselect where other_stuff = 'hello' although you shouldn't have any illusions about this being more efficient than writing the expression twice. regards, tom lane