Fero Fero - 7 months ago 18
SQL Question

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

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

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.

Comments