[英]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: 假设我有3张桌子:
**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"**.
我尝试使用
INSERT INTO...SELECT FROM
但是只有当**table1**
所有值均取自其他表而不是**"default_value"**.
时, **table1**
允许我输入值**"default_value"**.
I cannot use 2 different queries since all columns from table1
have NOT NULL
constraint on them. 我不能使用2个不同的查询,因为
table1
所有列都具有NOT NULL
约束。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.