183

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Fero
  • 12,969
  • 46
  • 116
  • 157

13 Answers13

339

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.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
jdias
  • 5,572
  • 4
  • 22
  • 25
102

If you don't want to list the fields, and the structure of the tables is the same, you can do:

INSERT INTO `table2` SELECT * FROM `table1`;

or if you want to create a new table with the same structure:

CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;

Reference for insert select; Reference for create table select

Bryan
  • 11,398
  • 3
  • 53
  • 78
22

You can easily get data from another table. You have to add fields only you want.

The mysql query is:

INSERT INTO table_name1(fields you want)
  SELECT fields you want FROM table_name2


where, the values are copied from table2 to table1

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
php
  • 4,307
  • 1
  • 24
  • 13
17
CREATE TABLE newTable LIKE oldTable;

Then, to copy the data over

INSERT INTO newTable SELECT * FROM oldTable;
slfan
  • 8,950
  • 115
  • 65
  • 78
Seymur Asadov
  • 612
  • 5
  • 19
4

The best option is to use INSERT...SELECT statement in mysql.

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

dexter.ba
  • 292
  • 2
  • 5
3
INSERT INTO Table1(Column1,Column2..) SELECT Column1,Column2.. FROM Table2 [WHERE <condition>]
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • 3
    While this code may answer the question, it would be better to explain how it solves the problem and why to use it. Code-only answers are not useful in the long run. – Tobias Liefke Nov 12 '15 at 15:59
3
SELECT *
INTO newtable [IN externaldb]
FROM table1;

http://www.w3schools.com/sql/sql_select_into.asp

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
mikey
  • 2,022
  • 1
  • 13
  • 6
  • 1
    Doesn't work. Per [the docs](http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html), "MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL extension" – Bryan Jul 27 '14 at 17:00
2

You should create table2 first.

insert into table2(field1,field2,...)
select field1,field2,....
from table1
where condition;
0

the above query only works if we have created clients table with matching columns of the customer

INSERT INTO clients(c_id,name,address)SELECT c_id,name,address FROM customer
Biddut
  • 418
  • 1
  • 6
  • 17
Qanuni
  • 1
  • 1
0

You can try this code

insert into #temp 
select Product_ID,Max(Grand_Total) AS 'Sales_Amt', Max(Rec_Amount) ,'',''
from Table_Name group by Id
Biddut
  • 418
  • 1
  • 6
  • 17
0

IF the table is existed. you can try insert into table_name select * from old_tale;

IF the table is not existed. you should try create table table_name like old_table; insert into table_name select * from old_tale;

Jac Tian
  • 15
  • 1
0

$sql_query= "REPLACE INTO customerdeletelist SELECT * FROM customerdetail WHERE account_number = '$account_number' && customer_number = '$customer_number' && updateDate = '$updateDate'";

$shift= mysqli_query($con, $sql_query);

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '22 at 00:23
0

If someone doing it regularly - u can try to use this tool. Unstable, but ready https://github.com/morkva-vladyslav/database-donor

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32984250) – theking2 Oct 24 '22 at 11:44