Solved

insert --select from Multiple database

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

822 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