在不同的数据库中连接多个表? MySql [英] Connect multiple tables in different databases? MySql

查看:314
本文介绍了在不同的数据库中连接多个表? MySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在寻找一个好方法连接到至少2个数据库在mysql使用php和收集表中的每个数据库中的信息。

Hey, I am looking for a good way to connect to at least 2 databases in mysql using php and gathering information within each of those databases from tables.

表将具有相关信息。例如。我在一个数据库('siteinfo')中的一个名为sites的表中有网站名称。我也在另一个数据库('sitedescriptions')在一个名为'descriptors'的表中有网站描述。

The tables will have related information. eg. I have site names in one database('siteinfo') in a table called 'sites'. I also have the site descriptions in another database('sitedescriptions') in a table called 'descriptions'.

我似乎不能得到这两个数据库彼此。有谁知道我怎么可以去做上述?我对php / mysql很新。

I can't seem to get these two databases to talk to each other. Does anyone know how I can go about doing the above? I'm pretty new to php/mysql.

此外,在'descriptions'表中有三行与sites表中的一个站点相关的信息。

Also, in the 'descriptions' table there are three rows of information that are related to one site in the sites table. is it possible to get those rows to talk with only the one site that it relates to?

感谢您的帮助。

推荐答案

一个好的策略可能是定义一个MySQL用户对两个数据库具有相同的访问权限。这样的东西:

A good strategy for you might be to define a single MySQL user that has the same access to both databases. Something like this:

GRANT ALL ON siteinfo.* TO user@localhost IDENTIFIED BY 'password';
GRANT ALL ON sitedescriptions.* TO user@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

一旦您以该用户身份建立连接,就可以访问这两个数据库,

Once you establish a connection as that user, you will have access to both databases, and can reference them easily by prefixing your table names with the database name.

SELECT * FROM siteinfo.sites;

您甚至可以毫无困难地以这种方式跨数据库加入表。因此,要回答第二个问题,要获取网站的所有三个描述行,您可以这样做(假设网站在每个数据库中具有相同的ids,或相同的名称,或者您可以加入的唯一的) p>

You can even join your tables across databases in this way with no difficulty. So to answer your second question, to get all three description rows for a site, you could do this (assuming the sites have the same ids in each database, or the same name, or something unique that you can join on):

SELECT * FROM
    siteinfo.sites AS s LEFT JOIN 
    sitedescriptions.description AS d ON s.siteId=d.siteId
WHERE s.siteId=123;

这篇关于在不同的数据库中连接多个表? MySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆