Re: insert into table with nest query

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: e-letter <inpost(at)gmail(dot)com>, pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: insert into table with nest query
Date: 2023-04-27 09:28:03
Message-ID: 4019c5de3435de8e0c702e34c15d7000b2046e5b.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Tue, 2023-04-25 at 16:43 +0100, e-letter wrote:
> table1
> column1,column2
> value1,value2
>
> table2
> column2,column3
> value2, value3
>
> table3
> column1,column2,column3
> value1,value2,value3
>
> 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?
>
> INSERT INTO table3 SELECT ... WHERE value3='...';
>
> 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 = 'value3';

Yours,
Laurenz Albe

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Shade Oyewole 2023-05-09 08:58:52 Server loading forever
Previous Message e-letter 2023-04-25 15:43:39 insert into table with nest query