Solved

insert --select from Multiple database

Posted on 2011-03-23
4
276 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now