Reputation: 5758
What is the best way to copy database from one to another with specific date?
Here is the flow, the user do some action on my program (PHP Language) and then get some file. Then that file he need to upload on the other program so the database copied.
What will be copied will is only the data without structure, since structure is always same.
Example here is the structure the data
Source table called old
id | date | value_1 | value_2 | value 3
1 | 2012-06-02 | some_data | some_data | some_data
2 | 2012-06-03 | some_data | some_data | some_data
3 | 2012-06-03 | some_data | some_data | some_data
and then the user want to download the data only for the 2012-06-03
to moved it into the new table called new
. And the result in the new
will be:
id | date | value_1 | value_2 | value 3
2 | 2012-06-03 | some_data | some_data | some_data
3 | 2012-06-03 | some_data | some_data | some_data
Notes:
[*]The program will be need smart enough to understand, if the `id` already in the `new` database then do `update` if not do `insert`.
[*]The table is located on different database. e.g: `db_source.old` copied to `db_target.new`
What is the best logic to do that? or maybe is there already some classes to handle this kind of task?
Please understand this is not college task, but real project implementation that i want to add into my program. And I'm asking here is not for get the code
question. Please give me some clue then i will be happy to research it
Already made some script that output the data.
Some snippet that do the gathering data script
/**
* Get all database data and output it in array format
* @return array
*/
function output(){
$return = array();
foreach($this->getAllTableName() as $table_name){
$this->load->model($table_name.'_model');
$class_name = $table_name.'_model';
$model = new $class_name;
$return[$table_name] = array();
$this->db->where('created >=', $this->date_start);
$this->db->where('created <=', $this->date_end);
foreach((array)$model->get() as $object){
$return[$table_name][$object->id] = $this->wrap_value($object);
}
}
return $return;
}
The output:
but still confused with the SQL file will be written to understand which data need to update
and insert
Upvotes: 0
Views: 282
Reputation: 2423
You can also create the table with the select response:
create table newtbl select * from oldtbl where date='2012-06-03';
The new table will inherit the field definition from the original table.
Upvotes: 0
Reputation: 5407
Do a stored procedure that accepts the id numbers (maybe in an array or something) and moves the data. Are the tables names hard-coded? Are the databases hard-coded? You can always write a stored procedure for each table and do the logic to choose which procedure in php. If the databases are not hard-coded you could always take the in as a parameter.
Not sure the exact coding but how about something like this:
Have the @ids passed as "id1, id2, id3..." or an array. Use them with the in clause. Also use insert-on-duplicate
INSERT INTO new.table (id,date,value_1, value_2, value_3)
select id, date, value_1, value_2, value_3
from old.table
where id in (@ids)
ON DUPLICATE KEY UPDATE c=3;
You just have to replace c=3 with your update. Here is an example of an update with join from here
UPDATE FROM tblTransaction AS t
LEFT JOIN tblEmployee as e
ON e.emp_id = t.emp_id
SET t.emp_block = e.emp_block
Upvotes: 3