简体   繁体   中英

Laravel 5.4 - copy data from one table to another table, in same database

Is there any way to copy entire data from one table to another table?

Condition: without making these data to retrieve from one table and storing to array than saving to another table.

Reason according to me:

  1. running these entire database on localhost
  2. there are nearly avg 100k rows
  3. retrieving to array is costly with respect to memory (don't care) but time (important, as entire db on localhost which process slowly)

To copy data from one table and also all the dependent objects of the table, you use the following statements:

CREATE TABLE IF NOT EXISTS new_table LIKE existing_table;

INSERT new_table
SELECT * FROM existing_table;

From here: MySQL Copy Table With Examples

$results = DB::select( DB::raw("CREATE TABLE tbl_new AS SELECT * FROM tbl_old;'") );

I believe you will get what you want.

comment on this answer if you need more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM