Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sayCx-00Cf8D-25 for pgsql-general@arkaria.postgresql.org; Mon, 05 Aug 2024 13:57:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sayCv-00E67p-74 for pgsql-general@arkaria.postgresql.org; Mon, 05 Aug 2024 13:57:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sayCu-00E67h-S8 for pgsql-general@lists.postgresql.org; Mon, 05 Aug 2024 13:57:04 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sayCo-003EDH-JM for pgsql-general@lists.postgresql.org; Mon, 05 Aug 2024 13:57:03 +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 475DuvC71064262; Mon, 5 Aug 2024 09:56:57 -0400 From: Tom Lane To: Dominique Devienne cc: pgsql-general@lists.postgresql.org Subject: Re: UPDATE-FROM and INNER-JOIN In-reply-to: References: Comments: In-reply-to Dominique Devienne message dated "Mon, 05 Aug 2024 13:33:36 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1064260.1722866217.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 05 Aug 2024 09:56:57 -0400 Message-ID: <1064261.1722866217@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dominique Devienne writes: > In https://sqlite.org/forum/forumpost/df23d80682 > Richard Hipp (Mr SQLite) shows an example of something > that used to be supported by SQLite, but then wasn't, to be > compatible with PostgreSQL. For the archives' sake: CREATE TABLE t1(aa INT, bb INT); CREATE TABLE t2(mm INT, nn INT); CREATE TABLE t3(xx INT, yy INT); UPDATE t1 SET bb =3D mm+xx FROM t2 INNER JOIN t3 ON nn=3Dxx AND mm=3Daa; yields ERROR: column "aa" does not exist LINE 1: ... t1 SET bb =3D mm+xx FROM t2 INNER JOIN t3 ON nn=3Dxx AND mm=3D= aa; ^ DETAIL: There is a column named "aa" in table "t1", but it cannot be refe= renced from this part of the query. > Thus I'm curious as to why PostgreSQL refuses the first formulation. > Could anyone provide any insights? Thanks, --DD This seems correct to me. The scope of the ON clause is just the relations within the INNER JOIN, which does not include t1. You would get the same from SELECT * FROM t1, t2 INNER JOIN t3 ON nn=3Dxx AND mm=3Daa; ERROR: column "aa" does not exist LINE 1: SELECT * FROM t1, t2 INNER JOIN t3 ON nn=3Dxx AND mm=3Daa; ^ because again t1 is not part of the JOIN sub-clause. (MySQL used to get this wrong, many years ago, and it seems that has taught a lot of people some strange ideas about syntactic precedence within FROM clauses. Postgres' behavior agrees with the SQL spec here.) regards, tom lane