简体   繁体   English

根据匹配字段,使用来自另一个数据库的数据更新MySQL数据库表

[英]Update MySQL database table with data from another database, based on matching field

Using PHP5, I'm needing to access two different databases (on the same server) but with different login credentials. 使用PHP5,我需要访问两个不同的数据库(在同一服务器上),但是具有不同的登录凭据。 Then get data from one database and update the other database for a record matching a unique field value. 然后从一个数据库获取数据,并更新另一个数据库以获得与唯一字段值匹配的记录。 Here's what I have so far... 到目前为止,这就是我所拥有的...

$link2->query("UPDATE db2.table2 
    INNER JOIN db1.table1 
        ON db1.table1.email = db2.table3.email 
SET db2.table2.field2 = db1.table1.field1");

Where I am totally lost is how to handle the fact that both databases have different connections. 我完全迷失的地方是如何处理两个数据库具有不同连接的事实。 Which connection to I query? 我要查询哪个连接? How do I incorporate the connection of the other database? 如何合并其他数据库的连接?

To use a single query to hit two different databases, two things must be true: 若要使用单个查询命中两个不同的数据库,必须满足以下两个条件:

  1. Those databases must be on the same MySQL server. 这些数据库必须位于同一MySQL服务器上。
  2. The username you use must have appropriate access to both databases, and all tables involved. 您使用的用户名必须对两个数据库以及所有涉及的表都具有适当的访问权限。

If these two conditions don't hold you need to retrieve the information to your program with a connection to one database, and then update the tables in the other database with its connection. 如果这两个条件都不成立,则需要通过与一个数据库的连接来检索程序信息,然后使用其连接来更新另一个数据库中的表。 That can sometimes be hard. 有时可能很难。

I believe your best bet in this case is to persuade your database administrator to add privileges to the account you use to modify db2 so it also has access to read db1 . 我相信,在这种情况下,最好的办法是说服数据库管理员向用于修改db2的帐户添加特权,以便它也可以读取db1 (Of course that won't help if they're not on the sane server.) (如果它们不在正常的服务器上,那当然无济于事。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM