[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

insert --select from Multiple database

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
SiemensSEN
Asked:
SiemensSEN
  • 2
2 Solutions
 
jrm213jrm213Commented:
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
 
tsmgeekCommented:
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
 
SiemensSENAuthor Commented:
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
 
tsmgeekCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now