【发布时间】:2012-12-31 18:21:51
【问题描述】:
我想将table1的内容复制到table2中,但不是直接复制,因为table2包含的列比table1多。结构类似这样:
表 1 { 第 2 列 第 4 列 第 6 列 }
表2 { 第 1 列 第 2 列 第 3 列 第 4 列 第 5 列 第 6 列 }
我想要做的是将 table1 中的每一行添加到 table2,并为缺少的列设置默认值。任何帮助将不胜感激。
【问题讨论】:
我想将table1的内容复制到table2中,但不是直接复制,因为table2包含的列比table1多。结构类似这样:
表 1 { 第 2 列 第 4 列 第 6 列 }
表2 { 第 1 列 第 2 列 第 3 列 第 4 列 第 5 列 第 6 列 }
我想要做的是将 table1 中的每一行添加到 table2,并为缺少的列设置默认值。任何帮助将不胜感激。
【问题讨论】:
你可以做一个
INSERT INTO xxx
SELECT yyy
并且在 select 子句中,放入默认值。
INSERT INTO Table2(column1, column2, column3, column4, column5, column6)
SELECT 'horse', column2, 2, column4, 'what ever I want', column6
FROM Table1
所以 int Table2,所有 column1 都将具有 'horse' 值。 所有 column3 将有 2。 等等。
【讨论】:
使用INSERT..INTO SELECT 声明
INSERT INTO table2 (column2, column4, column6)
SELECT column2, column4, column6
FROM table1
所以在这种情况下,列:column1、column2、column3 将具有空值。或您设置的任何默认值。
【讨论】:
不擅长 SQL 但类似这样:
INSERT INTO [Table1] (column2, column4, column6) SELECT (column1, column2, column3) FROM [Table2]
希望这会有所帮助。可能有用的链接,http://www.blackwasp.co.uk/SQLSelectInsert.aspx
投票给我我需要积分 :P
【讨论】:
请尝试
INSERT INTO
Table2 (
column1,
column2,
column3,
column4,
column5,
column6,
)
SELECT
'StaticValue1',
column2,
'StaticValue2'
column4,
'StaticValue3'
column6,
FROM
Table1
【讨论】:
将完整表复制到新表中:
SELECT * INTO table2 FROM table1;
http://www.w3schools.com/sql/sql_select_into.asp
将表复制到现有表中:
INSERT INTO table2
SELECT * FROM table1;
【讨论】:
insert into TABLE2
select null colum1, column2,null colum3,column4,null colum5,column6
from((
Select TABLE1.column2, TABLE1.column4, TABLE1.column6
from TABLE1, TABLE2
where TABLE1.primarykey=TABLE2.primarykey))
【讨论】: