Copy fields from one database to another in MySQL - php

I have already known that here is question like mine:
PHP/MySQL: Copy Table and Data from one Database to another
But, that question was asked about 4 years ago, so I would like to repeat it today.
INSERT INTO `wp_estate`.`wp_posts`(post_title)
SELECT `s_description` FROM `u519633785_armar`.`oc_t_item_description`
WHERE `fk_i_item_id` > 4;
How I can "see" two databases at the same time

You first have to create a table into database you want to copy the data.
To get the complete CREATE statement of table. Run Following query in your DB:
show create table table_name;
This will return you the complete create query of table.
After creating a table to your new database, you can copy all data from your old table to table of your new DB.
Query to copy data:
insert into newDB.your_table select * from oldDB.your_table;
Updated insert query:
INSERT INTO `wp_estate`.`wp_posts`(post_title)
SELECT `s_description` FROM `u519633785_armar`.`oc_t_item_description`
WHERE `fk_i_item_id` > 4;

Related

failed inserting a data from a table to another table with insert then select method

I have made a project but I have a problem when inserting data from a table to another table.
I use insert, then do select method here.
Last time I had to use that code worked well but after I add 1 more table in my db, the code no longer works. I just add more date table at my db. That date table contains the date now(). Here I use the date when I insert the data in my database.
I use the following code for inserting the data:
$sql="Insert into termocouple (no,tanggal,tanggal2,silo,sensor1,sensor2,sensor3,sensor4,sensor5,sensor6,sensor7,sensor8,sensor9,sensor10,sensor11,sensor12,ambien,average,deffiasi,status)
Select no,tanggal,now(),silo,sensor1,sensor2,sensor3,sensor4,sensor5,sensor6,sensor7,sensor8,sensor9,sensor10,sensor11,sensor12,ambien,average,deffiasi,status from temp1";

Getting SQL for already populated tables? PHP My Admin

Using MySQL I can run the query:
SHOW CREATE TABLE employee;
And it will return the create table statement for the specified table. This is useful if you have a table already created, and want to create the same table on another database.
Is it possible to get the insert statement for an already existing set of rows, so that if a blank db with the tables created is set up, the sql can be pasted into the query box & all the tables will be populated exactly the same as the original?
Thanks
Example of data populated in the table, retrieving the sql for this.
Check the following link for exporting DB from phpmyadmin
https://mediatemple.net/community/products/dv/204403864/export-and-import-mysql-databases

Is there a way to copy a table structure to all tables in MySQL database at once?

phpmyadmin allows you to create a new table from a current table by copying its strucutre/strucute+data/data only...etc through a window similar to this...
However is it possible to copy the structure of a table to multiple tables in your database at once ?
one option would be to export the structure of the table. Then copy the sql generated by phpadmin. You could create a php script that iterates over the table names and runs the same alter/create query but replaces the table name.
You can do this using the show TABLES; query in mysql to get the tables then run alter $row[0] ... or create $row[0] ... queries from the export to do the work for you. just replace the table name with the varable name you have set in your loop.

How to duplicate specific table content(given condition) into a new database table using php codeigniter

How to duplicate specific table content(given condition) into a new database table using php codeigniter ?
For example, I should get from db job table the following:
job_id, job_title, job_description, client_id provided that the job was already awarded by client to provider (i.e. "status"=="Awarded")
NOTE: I should be able to completely "copy/duplicate" these entries to avoid missing link if ever the specific job was later on deleted by client
provider_id , proposal_id from db job_proposal table (again, I think condition here is status==approved)
and then add NEW fields that are nowhere in the existing tables in my db..
I would appreciate any help.. thanks!
CREATE TABLE 'new_table_name' LIKE 'your_table_name';
Then
INSERT INTO New_table SELECT * FROM Old_table1,Old_table2 GROUP BY ID;
This will create a new table with all the datas.This command is used as a backup for the tables.
NOTE: If the second table doesn't have the same strucutre, u hv to first create the necessary columns, and then get the backup of the datas from the second table

Issue in copying rows from one table to another

I am implementing a request mechanism where the user have to approve a request. For that i have implemented a temporary table and main table. Initially when the request is added the data will be inserted to the temporary table, on approval it will be copied to the main table.
The issue is there will be more than 5k rows to be moved to the main table after approval + another 3-5 row for each row in the detail table (stores the details).
My current implementation is like this
//Get the rows from temporary table (batch_temp)
//Loop through the data
//Insert the data to the main table (batch_main) and return the id
//Get the details row from the temporary detail table (batch_temp_detail) using detail_tempid
//Loop through the data
//Insert the details to the detail table (batch_main_detail) with the main table id amount_id
//End Loop
//End Loop
But this implementation would take atleast 20k queries. Is there any better ways to implement the same.
I tried to create a sqlfiddle but was unable to create one. So i have pasted the query in pgsql.privatepaste.com
I'm sorry that I'm not familiar with PostgreSQL. My solution is in MySQL, I hope it can help since if they (MySQL & PostgreSQL) are same.
First, we should add 1 more field into your batch_main table to track the origin batch_temp record for each batch_main record.
ALTER TABLE `batch_main`
ADD COLUMN tempid bigint;
Then, on approval, we will insert 5k rows by 1 query:
INSERT INTO batch_main
(batchid, userid, amount, tempid)
SELECT batchid, userid, amount, amount_id FROM batch_temp;
So, with each new batch_main record we have its origin batch_temp record's id. Then, insert the detail records
INSERT INTO `batch_main_detail`
(detail_amount, detail_mainid)
SELECT
btd.detail_amount, bm.amount_id
FROM
batch_temp_detail `btd`
INNER JOIN batch_main `bm` ON btd.detail_tempid = bm.tempid
Done!
P/S:
I'm confuse a bit about the way you name your fields, and since I do not know about PostgreSQL and by looking into your syntax, can you use same sequence for primary key of both table batch_temp & batch_main? If you can, it's no need to add 1 more field.
Hope this help,
Simply need to update your Schema. Instead of having two tables: one main and one temporary, you should have all the data in main table, but have a flag which indicates whether a certain record is approved or no. Initially it will be set to false, and once approved it will simply be set to true and then the data can display on your website etc. That way you will not need to write the data two times, or even have to move it from one table to another
You haven't specified RDBMS you are using, but good old INSERT with SELECT in it must do the trick in one command:
insert main (field1,...,fieldN) select field1,...,fieldN from temporary

Categories