Ask Your Question
0

How can I choose data from the initial table and subsequently input it as a parameter in the second table in PGSQL?

asked 2023-05-25 02:03:50 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-25 02:21:01 +0000

scrum gravatar image

You can choose data from the initial table using a SELECT statement and then use it as a parameter in the second table by using it in an INSERT statement. Here is an example:

Assuming you have two tables named "initialtable" and "secondtable" with the following structures:

initial_table: id | name | age ---|------|---- 1 | John | 25 2 | Jane | 30

secondtable: id | personid | salary ---|-----------|------- 1 | | 2 | |

You can choose the data from the "initialtable" and insert it as a parameter in the "secondtable" using the following SQL query:

INSERT INTO second_table (person_id, salary)
SELECT id, 50000 FROM initial_table
WHERE age > 25;

This query will insert a new record in the "secondtable" with the personid of the record from the "initial_table" where age is greater than 25 and a salary of 50000.

Note: You can replace the WHERE clause with any other condition you want to choose the data from the "initial_table".

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-25 02:03:50 +0000

Seen: 7 times

Last updated: May 25 '23