Solved

insert --select from Multiple database

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 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