Unbreakable
Unbreakable

Reputation: 8084

Copy data from one column to another in MySQL

I have a table 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

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.

Upvotes: 0

Views: 2034

Answers (2)

mxlse
mxlse

Reputation: 2784

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

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

Upvotes: 1

Ranadip Dutta
Ranadip Dutta

Reputation: 9123

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

Upvotes: 1

Related Questions