How to copy data from one table to another new table in MySQL?

Fero picture Fero · Sep 20, 2011 · Viewed 271.6k times · Source

I want to copy data from one table to another in MySQL.

Table 1 (Existing table):

aid    
st_id
from_uid
to_gid
to_uid
created
changed
subject
message
link

Table 2 (New Table)

st_id
uid
changed
status
assign_status

I want to copy some fields of data from TABLE 1 into TABLE 2.

Can this be done using MySQL queries?

Answer

jdias picture jdias · Sep 20, 2011

This will do what you want:

INSERT INTO table2 (st_id,uid,changed,status,assign_status)
SELECT st_id,from_uid,now(),'Pending','Assigned'
FROM table1

If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.

I hope this helps.