Solved

insert --select from Multiple database

Posted on 2011-03-23
4
282 Views
Last Modified: 2012-06-27
Hello,
  I have a 2 database and I would like to select data from one table and insert it into another table  in the other database

insert into db1.tbl1
select  distinct tbl2* from db2.tbl2

How can I do this with PHP.

Thanks
0
Comment
Question by:SiemensSEN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 75 total points
ID: 35199048
Hi,

The only way I could see that working is if the databases were on the same server and had the same username and password (basically sharing a connection...) then you could do exactly what you have there. Otherwise as far as I know you cannot do what you are asking.

So basically, open the connection to the first database (database1), and then only apply the db name for instances of the 2nd database (database2)...


from http://dev.mysql.com/doc/refman/5.0/en/insert-select.html (if you are using a different db this may be different)

INSERT INTO database2.table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2;

0
 
LVL 5

Assisted Solution

by:tsmgeek
tsmgeek earned 50 total points
ID: 35201695
If the databases are both onthe same machine then you can run the command as you mentioned as long as the colls are the same, if they are on diffrent machines then your work is harder,

if we are talking lots of data then look here
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
or
make two db connection
do a select all on the table1.db1
then run an insert in table1.db2 for each record you got from table1 or you can do a bulk insert by concatinating all the values but this way you may end up reaching the command limit

if its the odd record then just read the record then write it out in a loop
0
 

Author Comment

by:SiemensSEN
ID: 35201814
Thanks.
  I have system with lots of data .  Some of the information is private. What I wanted to do was to copy only the data that are public to the second database. The structure of the first and second DB is the same
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35203663
which method do you need to do then out of what ive listed or does it not fit the bill
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question