ChatGPT解决这个技术问题 Extra ChatGPT

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?

Is this a one time job or you plan to do it regularly?
@@jdias: until now it's one time job..
@jdias To clarify, if it's not a one time job, what should a MySQL noob look into doing instead?
Probably views to avoid duplicating data.
Possible duplicate of Copy data into another table

P
Phil Dukhov

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.


Great, glad to see this is the same as what I've been used to in T-SQL.
If You want to copy all table1 data then you must create new table2 before copy @SANDEEP
B
Bryan

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


why [AS] is in square brackets, what is done by AS in here
It denotes that the word is optional. It is written that way because I copied and pasted from the docs; it does not add anything. I think the AS is mandatory in other SQL dialects.
The bracket should be removed for it to perform the task
B
Brad Larson

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


s
slfan
CREATE TABLE newTable LIKE oldTable;

Then, to copy the data over

INSERT INTO newTable SELECT * FROM oldTable;

d
dexter.ba

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

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


Intuitive :) I like it. It is a normal SELECT statement, preceded with one line of "INSERT INTO".
E
Eric Leschinski
SELECT *
INTO newtable [IN externaldb]
FROM table1;

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


Doesn't work. Per the docs, "MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL extension"
N
Nana Partykar
INSERT INTO Table1(Column1,Column2..) SELECT Column1,Column2.. FROM Table2 [WHERE <condition>]

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.
S
Sriyashree Swain

You should create table2 first.

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

B
Biddut

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

B
Biddut

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

J
Jac Tian

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;


M
Muhammad Zahid

$sql_profile_list = "REPLACE INTO customerdeletelist SELECT * FROM customerdetail WHERE account_number = '$account_number' && customer_number = '$customer_number' && updateDate = '$updateDate'"; $delete= mysqli_query($con, $sql_profile_list);


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.