Thursday, 16 October 2014

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.

Example solution:
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.

#2:
INSERT INTO table_name1(fields you want)
  SELECT fields you want FROM table_name2
#3:
INSERT INTO `table2` SELECT * FROM `table1`;











0 comments:

Post a Comment