V
V
Vladislav2016-04-07 20:49:42
Laravel
Vladislav, 2016-04-07 20:49:42

How to copy rows from one table to another?

How it is better to make such rewriting from one table in another?
There are 2 tables:

Wallets:
...| user_id | type | money | ... |
Stats:
..| user_id | currency | money |

Maybe somehow you can get and change the collection?
$wallets = Wallets::all();
// здесь $wallets  нужно сохранить в Stats

This script will be executed once a day by cron/schedule task
.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DieZz, 2016-04-07
@DieZz

DB::raw("INSERT INTO Wallets (user_id, type, money) 
    SELECT user_id, currency, money FROM Stats")

A
Alexey Ukolov, 2016-04-07
@alexey-m-ukolov

The obvious problem is that your columns in the tables do not match.
In general, your problem can be solved with one query

INSERT INTO table1 (column1) SELECT col1 FROM table2

In addition, you can use the Query Builder 'th:
DB::table('users')->insert([
    ['email' => '[email protected]', 'votes' => 0],
    ['email' => '[email protected]', 'votes' => 0]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question