[英]How to select two columns from two tables and insert them to single table with two columns [closed]
我使用了以下代码,但无法正常工作。
我想要的值插入table2.col1
成列1和值table3.col2
到表1的列1。
INSERT INTO table1 ( column1,column2 )
SELECT col1 FROM table2 ,
SELECT col2 FROM table3
尚不清楚如何将源表中的数据合并到目标表中,但这是一种方法:
INSERT INTO table1 (column1, column2)
SELECT col1, NULL
FROM table2
UNION ALL
SELECT NULL, col2
FROM table3
;
这将所有行复制从table2
使用table2.col1
值table1.column1
和NULL
的table1.column2
。 它还将包括从所有行table3
使用NULL
为table1.column1
和table3.col2
的值table1.column2
。
假设table2.col1
和table3.col2
的数据类型相同/兼容。
您可能还需要强制转换NULL
值,以便数据类型匹配: CAST(NULL AS <data_type>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.