[英]How to move certain column data from 1st table to 2nd table. And filling other columns of 2nd table with different data in a single query
I know about this query: 我知道这个查询:
INSERT INTO table2 (column1, column2, column3)
SELECT column1, column2, column3,
FROM table1
WHERE condition;
Let's say, I have at least 5 columns in table2. 假设,表2中至少有5列。 I want to get data of first three columns from table1 for table 2. I can do this with the above query.
我想从表1的表2中获取前三列的数据。我可以使用上述查询来完成此操作。 But I also want to fill other 2 columns of table2 with some data that is stored in variables.
但是我也想用存储在变量中的一些数据填充table2的其他2列。 Is there any way to do so?
有什么办法吗?
I want something like this: 我想要这样的东西:
INSERT INTO table2 (column1, column2, column3,column4,column5)
VALUES(
SELECT column1, column2, column3,
FROM table1
WHERE condition
, @dataForColumn4, @dataForColumn5)
You just include them in the SELECT
: 您只需将它们包括在
SELECT
:
INSERT INTO table2 (column1, column2, column3, column4, column5)
SELECT column1, column2, column3, @column4, @column5
FROM table1
WHERE condition;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.