Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQSmz-0007r0-8e for pgsql-admin@arkaria.postgresql.org; Fri, 12 Apr 2013 01:28:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UQSmy-0002Vh-PU for pgsql-admin@arkaria.postgresql.org; Fri, 12 Apr 2013 01:28:28 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQSmx-0002VX-Om for pgsql-admin@postgresql.org; Fri, 12 Apr 2013 01:28:27 +0000 Received: from ipmail06.adl6.internode.on.net ([2001:44b8:8060:ff02:300:1:6:6]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UQSmt-0005Se-SY for pgsql-admin@postgresql.org; Fri, 12 Apr 2013 01:28:26 +0000 Received: from mx.glasslawyers.com (HELO [192.168.2.1]) ([59.167.189.186]) by ipmail06.adl6.internode.on.net with ESMTP; 12 Apr 2013 10:58:18 +0930 Message-ID: <51676326.9040807@internode.on.net> Date: Fri, 12 Apr 2013 11:28:06 +1000 From: Julian Glass User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: Re: Invalid SQL not rejected? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -4.3 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org On 12/04/13 10:17, Craig James wrote: > Hmmm.... the subselect is invalid, but not rejected. The outer select > returns every row in the customer_order_matches table. This seems > pretty wrong. This is PG 9.2.1 running on Ubuntu. > > > db=> select count(1) from customer_order_matches where > customer_order_item_id in (select customer_order_item_id from > customer_order where customer_order_id = 105862140); > count > ------- > 36180 > (1 row) > > db=> select customer_order_item_id from customer_order where > customer_order_id = 105862140; > ERROR: column "customer_order_item_id" does not exist > LINE 1: select customer_order_item_id from customer_order where cust... > ^ > > Craig The "customer_order_item_id" in the subselect is referencing the column and therefore the value of the outer query "customer_order_item_id" in the query result. i.e: SELECT generate_series FROM generate_series(1,100) WHERE generate_series IN (SELECT generate_series); Which is always going to be true. (x == x). You might want to explicitly reference tables (and alias). Such as: SELECT count(*) FROM customer_order_matches com WHERE com.customer_order_item_id IN (SELECT co.customer_order_item_id FROM customer_order co WHERE co.customer_order_id = 105862140); Although I'm making asumptions with the "customer_order_id". Jules. -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin