[英]Transfer data from one table to another along with other values (SQL)
我想将多个表中的数据以及一些默认值插入到一个表中。
假设我有3张桌子:
**table1**(col1, col2, col3, col4)
**table2**(col5, col6)
**table3**(col7)
我想要
insert in table1.col1 -> table2.col5,
table1.col2 -> table2.col6,
table1.col3 -> table3.col7,
table1.col4 -> 'default_value' for some WHERE condition.
我尝试使用INSERT INTO...SELECT FROM
但是只有当**table1**
所有值均取自其他表而不是**"default_value"**.
时, **table1**
允许我输入值**"default_value"**.
我不能使用2个不同的查询,因为table1
所有列都具有NOT NULL
约束。
谁能帮我解决这个问题?
您尚未显示尝试过的查询,它可以帮助那些试图帮助您避免建议您尝试过的内容的人。 但是,尽管不知道您尝试了什么,但这应该可以工作-对我来说确实可以。
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.