Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jla8W-000471-L2 for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Jun 2020 15:38:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jla8V-000301-FW for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Jun 2020 15:37:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jla8V-0002xF-8K for pgsql-bugs@lists.postgresql.org; Wed, 17 Jun 2020 15:37:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jla8P-0003lW-1B for pgsql-bugs@lists.postgresql.org; Wed, 17 Jun 2020 15:37:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 05HFbn9Y1446961; Wed, 17 Jun 2020 11:37:52 -0400 From: Tom Lane To: "=?gb18030?B?trqxyMfrwLS1xLrv19M=?=" cc: "=?gb18030?B?cGdzcWwtYnVncw==?=" Subject: Re: SELECT * FROM a WHERE id IN (sub query) . When sub query is incorrect . return all rows of table a In-reply-to: References: Comments: In-reply-to "=?gb18030?B?trqxyMfrwLS1xLrv19M=?=" message dated "Wed, 17 Jun 2020 22:56:10 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1446959.1592408269.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 17 Jun 2020 11:37:49 -0400 Message-ID: <1446960.1592408269@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "=3D?gb18030?B?trqxyMfrwLS1xLrv19M=3D?=3D" writes: > When I have 2 tables like this = > create table test > ( > id int4, > shapeid int4 > ); > table testb > ( > id int4 > ); > sql " select shapeid from testb; " Failed. = = > but sql " select * from test where shapeid in ( SELECT shapeid FROM te= stb ); " return all records of table test = This is not a bug, it is behavior required by the SQL standard. Since shapeid doesn't exist in testb, shapeid in the sub-SELECT is just an outer reference to the column in the outer table. > sql like this delete all records of my talbe ! Yeah, it's a common trap for SQL newbies :-(. The standard advice is to always qualify column names in sub-selects, to be sure of where they are coming from. If you'd written ... where shapeid in ( SELECT testb.shapeid FROM testb ) ... then you'd have gotten an error message. regards, tom lane