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 1prxv7-0004jg-7u for pgsql-novice@arkaria.postgresql.org; Thu, 27 Apr 2023 09:28:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1prxv6-00073K-4p for pgsql-novice@arkaria.postgresql.org; Thu, 27 Apr 2023 09:28:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1prxv5-00073B-S6 for pgsql-novice@lists.postgresql.org; Thu, 27 Apr 2023 09:28:07 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1prxv3-005Ljf-AP for pgsql-novice@lists.postgresql.org; Thu, 27 Apr 2023 09:28:07 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-504eac2f0b2so14282094a12.3 for ; Thu, 27 Apr 2023 02:28:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1682587684; x=1685179684; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=/2pS81WH2ulihckD41v5nZ/iYcaiJP/AXaG5dT2FstI=; b=v4RpB6Hw9AaPvz2mgYasbMD/O96YPJZ1e7hki3RZI47Bw3Qq405u0cQli2yvRy136p m7kDeaU/XF4FZG1ozJ6X4t4t45OGBkb8sMYbgg0KvC6h18NAqnhST+5eQcVWs0lDvZ6m evcrw0nufCeHCGaSoL4CMZecXjxvbXpyMvimF/zfONT+go/azvhmibcR7ZrrGZu789wJ cLZRkhoVXdkvobyRZoLaAMQNymtB4C42hiUSgclsInnA7XHTxCHXGagFlb5fpG517+zu gR4FuKYHkKD4ruEbJH5x7HqmwSoYjrloc7PShwyIqhSuRoBxBwIPMuagdtrMV5KQRpiu u7OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682587684; x=1685179684; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=/2pS81WH2ulihckD41v5nZ/iYcaiJP/AXaG5dT2FstI=; b=Fyx0cu4K3DQtDNTlvUZeAX4voXTbLj5Bme/7GX6IQHkhG6U8wWhfDuWjYZL0kwkE+x K9XhCQMEVpevPhivlRqhFnQozo4rzbnwd82roVo3BF1m3Y3zK7Aqa6CgW7i+bvdAHJZR oWPcgUjc45MtB6mAxspLLDflJ4ontjh/NvWkHB9q5DzISZsboqyMgqakJ7MFPGgPsvdj V9vwOeB64kA0GS6BtA2dFJK6S4N9HToOsUZmVp8wUO2LTP8pYKWOlt88zqvZMqDvq69Q MFG8QKaTcPFtn4zVnuUIIZVNw5uGtdzm8iKPRaeBC2Or4LMr1gbH5dZ36b2dcwOdTohj bLiw== X-Gm-Message-State: AC+VfDxPDGl5XscUw009J3du6cXgR3ag4uzhp6KkpNXqi0zj5d06/3Dd faitjyPcUUUJiW7xqGmSjcnbXhV55HtwhhiHV+2sNA== X-Google-Smtp-Source: ACHHUZ6b0P/c9vAAHgBGv9n5uEp/EHey0dsVTfcfJbHipEi7U1Ww2u63pg3C+iH1Qqxx8Tfmb1ZTmw== X-Received: by 2002:a17:907:2da9:b0:95e:d74c:b06 with SMTP id gt41-20020a1709072da900b0095ed74c0b06mr1021123ejc.64.1682587684018; Thu, 27 Apr 2023 02:28:04 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:387b:5a4:e99:4794:568d]) by smtp.gmail.com with ESMTPSA id x3-20020a170906710300b0094f2f0c9ed9sm9240459ejj.167.2023.04.27.02.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 02:28:03 -0700 (PDT) Message-ID: <4019c5de3435de8e0c702e34c15d7000b2046e5b.camel@cybertec.at> Subject: Re: insert into table with nest query From: Laurenz Albe To: e-letter , pgsql-novice@lists.postgresql.org Date: Thu, 27 Apr 2023 11:28:03 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2023-04-25 at 16:43 +0100, e-letter wrote: > table1 > column1,column2 > value1,value2 >=20 > table2 > column2,column3 > value2, value3 >=20 > table3 > column1,column2,column3 > value1,value2,value3 >=20 > When value3 is first entered into table2, what are the correct > commands for value2 (in table2) and value1 (in table1) to be selected > for insertion into table 3? >=20 > INSERT INTO table3 SELECT ... WHERE value3=3D'...'; >=20 > The conceptual difficulty is how to use nested query correctly, or is > there a better method? Thank you. The question is a bit unclear, but perhaps you mean INSERT INTO table3 (column1, column2, column3) SELECT table1.column1, table1.column2, table2.column3 FROM table1 JOIN table2 USING (column2) WHERE table2.column3 =3D 'value3'; Yours, Laurenz Albe