简体   繁体   English

如何将某些列数据从第一表移动到第二表。 并在单个查询中用不同的数据填充第二张表的其他列

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

相关问题 如何在同一选择语句中从第一张表的列值中选择第二张表的数据? - How to select data of 2nd table from column values of 1st table in same select statement? Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table 是否可以使用PHP PDO从第一张表中的列从第二张表中获取数据? - Is it possible to get data from the 2nd table using column in 1st table using PHP PDO? Codeigniter联接3个表并从第1个表获取数据,其中第1个表与第2个表链接,第2个表与第3个表链接 - codeigniter join 3 tables and get data from 1st table where 1st table is linked with 2nd table and 2nd table is linked with 3rd table 如何从第二个表中显示2个不同的ID从第一个表中显示ID - How to show id from 1st table from 2 different ids from 2nd table mySQL 如何从第一个表中选择不同的(1 列:pruduct_Type)并从第二个表中选择 2 列 - mySQL How to select distinct(1 column : pruduct_Type) from 1st table and select 2 columns from 2nd table 如何使用PHP偏移HTML表以从不同列的第二行单元格值中减去第一行的单元格值 - How To offset an HTML table with PHP to Subtract cell value of 1st row from 2nd row cell value of different columns 如果找不到第二表行,如何显示第一行 - how to show 1st row if 2nd table row is not found PHP,mysql和两个数据库 - 使用不同的模式从第一个数据库中的表更新第二个数据库表中的某些行 - PHP, mysql & two databases - updating certain rows in 2nd database table from table in 1st db with different schema
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM