在MySQL中将数据从一列复制到另一列

时间:2022-06-06 15:09:49

I have a table A

我有一张桌子A.

col-PK  col2  col3   col4
1       a      aa     aaa
2       b      bb     bbb

I have created a new table B with three columns only

我创建了一个只有三列的新表B.

col-PKB  colOne  ColTwo  

I want below as the Final Output

我想在下面作为最终输出

Table A

col-PK  col2  col3   col4
1       a      aa     aaa
2       b      bb     bbb

Table B

col-PKB  colOne  ColTwo  
1       a       aa     
2       b       bb 

Solution I looked into SO LINK. But I think I need to use select statement as I have multiple columns to copy. Please guide me here. I am lost.

解决方案我查看了SO LINK。但我想我需要使用select语句,因为我有多列要复制。请指导我。我搞不清楚了。

2 个解决方案

#1


1  

You can use INSERT INTO with a SELECT-query of the columns you want to add:

您可以使用INSERT INTO对要添加的列的SELECT查询:

INSERT INTO tableB (col-PKB, colOne, ColTwo)
  SELECT
    col-PK,
    col2,
    col3
  FROM tableA;

#2


1  

Try like this:

试试这样:

INSERT INTO table (column)
  SELECT a_column 
  FROM a_table

In your case,

在你的情况下,

INSERT INTO tableB (
col-PKB, colOne, ColTwo
)
SELECT col-PK, col2, col3 
FROM tableA

#1


1  

You can use INSERT INTO with a SELECT-query of the columns you want to add:

您可以使用INSERT INTO对要添加的列的SELECT查询:

INSERT INTO tableB (col-PKB, colOne, ColTwo)
  SELECT
    col-PK,
    col2,
    col3
  FROM tableA;

#2


1  

Try like this:

试试这样:

INSERT INTO table (column)
  SELECT a_column 
  FROM a_table

In your case,

在你的情况下,

INSERT INTO tableB (
col-PKB, colOne, ColTwo
)
SELECT col-PK, col2, col3 
FROM tableA