Mysqli连接来自2个不同数据库的表 [英] Mysqli join tables from 2 different databases

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

问题描述

如何使用这2个连接来运行将2个表联接在一起的查询? yesno.table1和sushi.table1吗?假设我们通过id加入,它们都具有相同的ID. 我该怎么办?

How Can I use this 2 connections to run a query which joins 2 tables? yesno.table1 and sushi.table1 ? lets say we join by id they both have the same id. how can I do that?

// Connect to Yesno Database
$this->yesno_db = new mysqli("red", "yesno", "***", "yesnotmp");

if ($this->yesno_db->connect_errno) {
    throw new Exception('Connection failed: '.$this->yesno_db->connect_error);
}   

// Connect to Sushi Database
$this->sushi_db = new mysqli("red", "sushi", "***", "sushi");

if ($this->sushi_db->connect_errno) {
    throw new Exception('Connection failed: '.$this->sushi_db->connect_error);
}

推荐答案

此问题与mysqli(或任何其他API)无关.

This question has nothing to do with mysqli (or any other API).

要对来自不同数据库的表进行联接,连接到mysql的用户必须对两个数据库都具有SELECT权限.

To do a join with a table from different database, a user which connects to mysql, have to have SELECT rights for both databases.

完成此操作后,只需在连接语句中选择一个数据库,然后使用常规的点语法即可寻址另一个数据库:

Having this done, just select one of databases in your connection statement and address another using usual dot syntax:

SELECT * FROM t1 JOIN db2.t2

要从字面上回答您的问题,

To answer your question literally,

我可以使用这2个连接来运行连接2个表的查询吗?

Can I use this 2 connections to run a query which joins 2 tables?

你不能

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

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