php sql 将来自不同数据库的多个表连接在一起

php sql join together multiple tables from different databases(php sql 将来自不同数据库的多个表连接在一起)
本文介绍了php sql 将来自不同数据库的多个表连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有 3 个数据库,现在我需要将每个数据库中的多个表连接到单个查询中.我该怎么做?

I have 3 databases, and now I need to join several tables from each one of them into a singel query. How do I do this?

这是我的连接:

try {
    $con_options = array(
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  // _SILENT (pub) || _WARNING || _EXCEPTION  (dev)
    );

    $con_1 = new PDO('mysql:host=localhost; dbname=database_1', 'user_1', 'pass_1', $con_options);
    $con_2 = new PDO('mysql:host=localhost; dbname=database_2', 'user_2', 'pass_2', $con_options);
    $con_3 = new PDO('mysql:host=localhost; dbname=database_3', 'user_3', 'pass_3', $con_options);

} catch (PDOException $err) { 
    //  catch, record/log and do stuff with errors
}

我有 3 个不同的用户,每个数据库都有一个唯一的密码.一个数据库存储 facebook-apps 和其他 iframe 应用程序的应用程序数据.另一个保存所有网上商店数据,如产品、订单、客户等,而第三个保存网站结构和内容.

I have 3 different users with a unique password for each database. One database stores application-data for facebook-apps and other iframe applications. Another one holds all webshop data like products, orders, customers etc. while the third one holds site structure and content.

现在;我想以某种方式在单个查询中 JOIN 将它们三个放在一起.

Now; I would like to JOIN the three of them together in a single query somehow.

在我写这个问题的时候;我的一个想法是让另一个超级"用户可以访问所有三个数据库,并且只执行常规的多表查询?这是一个可以接受的解决方案吗?

While I was writing this question; One idea I got was to have another "super"-user with access to all three databases and just do a regoular multi table query? Would that be an acceptable solution?

如果是这样,我是否还必须在查询中指定哪个数据库?

If so, do I have to specify which database aswell in the query?

推荐答案

您需要一个可以访问所有三个数据库的用户.

You will need a user that has access to all three databases.

您将通过指定完整的表名将它们连接在一起,如下所示:

You will JOIN them together by specifying full table name, something like this:

SELECT * FROM database_1.logs AS d1 LEFT JOIN database_2.users AS d2 
  ON d1.username = d2.username ORDER BY d1.timestamp DESC LIMIT 10

这篇关于php sql 将来自不同数据库的多个表连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)