Skip to content Skip to sidebar Skip to footer

Copy A Table To Another Table With Different Structure

I have 2 tables in the DB: Table1 Table1_Temp The Table1_Temp was generated from a CSV. it is almost identical to Table1 but different because all Table1_Temp fields are VARCHAR a

Solution 1:

Choose the columns to use and cast them to the necessary type in your select

insertinto table1 (col1, col2, col3)
selectcast(col1 as signed), col5, col7
from Table1_Temp

Solution 2:

if both tables are on a different database (and different column)

INSERT INTO db1.table1 (Acol1, Acol2, Acol3)

SELECT Bcol1 AS Acol1, Bcol2 AS Acol2, Bcol3 AS Acol3

FROM db2.table1_temp 

This will only work if both databases are under 1 server (in this instance "localhost")

Post a Comment for "Copy A Table To Another Table With Different Structure"