MySQL copies data from one database to a table in another database. The two tables have the same structure

If you want to copy the data in a table in one database to another database or the same database, you can take the following methods: 

1. Tables with the same table structure, and in the same database (eg, table1, table2) 

Sql: insert into table1 select * from table2 (full copy) 

      insert into table1 select distinct * from table2 (do not copy duplicate records) 

      insert into table1 select top 5 * from table2 (top five records) 

2. Tables are not in the same database (eg, db1 table1, db2 table2) 

sql : insert into db1.table1 select * from db2.table2 (full copy) 

     insert into db1.table1 select distinct * from db2.table2 (do not copy duplicate records) 

     insert into tdb1.able1 select top 5 * from db2.table2 (the first five record) 

first build the table, and execute the statement, it's ok!
The table can also be copied. For the table to be copied, click 3 Info, various information will be displayed, select DDL Information, copy the following statement to the above Query, remember to select the database as the target database, and execute the statement!
Remember db1.table1. Can not save Kazakhstan! ! You can enter as many points as you want, but in the end it's all one point!



If the structures of the two tables are different, and you want to select certain items as the objects to be inserted, you can refer to the following statement to rewrite: 
INSERT into db_ds_edu.t_exam_student(stu_id,stu_number,stu_name,school_id,campus_id,grade_id,class_id,xuebu_id) 
  SELECT stu.id 
     , stu.student_num 
    ,person.name 
     , stu.school_id 
     , stu.campus_id 
     , stu.grade_id 
     , stu.clas_id 
     , stu.xuebu_id 
FROM 
  db_ds.t_stu_info AS stu 
    ,db_ds.t_p_person_info as person 
    WHERE stu.person_id = person.id 

is to select some items inserted into a table of a database, and then select data from the data table of the target database, Just match it up

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043648&siteId=291194637