简体   繁体   中英

Transfer data from one table to another along with other values (SQL)

I want to insert data from several tables into one table along with some default values.

Suppose I have 3 tables:

**table1**(col1, col2, col3, col4)
**table2**(col5, col6)
**table3**(col7)

I want to

insert in table1.col1 -> table2.col5, 
table1.col2 -> table2.col6,

table1.col3 -> table3.col7,

table1.col4 -> 'default_value' for some WHERE condition. 

I tried using INSERT INTO...SELECT FROM but that allows me to enter values only when all the values for **table1** are taken from other tables and not for **"default_value"**. I cannot use 2 different queries since all columns from table1 have NOT NULL constraint on them.

Can anyone help me how to forward with this?

You haven't shown what query you have tried which helps those people trying to help you to avoid suggesting something you have already tried. However, despite not knowing what you HAVE tried this should work - it certainly does for me.

INSERT INTO table1 (col1, col2, col3, col4)
SELECT t2.first_name, t2.age, t3.salary, 'DEFAULT VALUE'
FROM   table2 t2
       INNER JOIN
       table3 t3
       ON t2.rec_id = t3.rec_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM