简体   繁体   English

将数据与其他值一起从一个表传输到另一个表(SQL)

[英]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.

相关问题 将数据从一个表复制到另一个表(以及一些其他数据) - Copying data from one table to another (along with some other data) 将sql查询中的值与其他数据一起插入表列 - Inserting values from sql query into a table column along with other data SQL Server 2005:将数据从一个表转移到另一个表 - SQL Server 2005: Transfer data from one table to other 将数据从一个表转移到另一表 - Transfer data from one table to another table 如何从另一个表中插入值以及SQL Server中的其他值? - How to insert values from a table in another along with other values in sql server? 从一个表返回数据以及从另一个表映射数据的SQL最佳方法 - SQL Best way to return data from one table along with mapped data from another table 插入时将数据从一个表转移到另一个表 - Transfer data from one table to another on insert 选择一个表与另一个表的否定以及oracle主表sql查询中的数据 - Select Negation of one table from another along with data in master table sql query for oracle 如何将多行从一个表转移到另一个表 (SQL) - How to transfer several rows from one table to other table (SQL) SQL Server:将数据从一个表传输到另一个表[内存分配失败] - SQL Server: Transfer data from one table to another [Memory Allocation Failure]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM